-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathasync_mlp.rs
More file actions
144 lines (132 loc) · 4.79 KB
/
async_mlp.rs
File metadata and controls
144 lines (132 loc) · 4.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
* SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/
use cuda_async::device_context::global_policy;
use cuda_async::device_operation::*;
use cutile::api::copy;
use cutile::tensor::{Partition, Tensor, ToHostVec, Unpartition};
use cutile::tile_kernel::{IntoDeviceOperationPartition, TileKernel};
use cutile::{api, error::Error};
use tokio::task::JoinHandle;
#[cutile::module]
pub mod my_kernels {
use cutile::core::*;
#[cutile::entry()]
pub fn gemm<const BM: i32, const BN: i32, const BK: i32, const K: i32>(
z: &mut Tensor<f32, { [BM, BN] }>,
x: &Tensor<f32, { [-1, K] }>,
y: &Tensor<f32, { [K, -1] }>,
) {
let part_x = x.partition(const_shape![BM, BK]);
let part_y = y.partition(const_shape![BK, BN]);
let pid: (i32, i32, i32) = get_tile_block_id();
let mut tile_z = z.load();
for i in 0i32..(K / BK) {
let tile_x = part_x.load([pid.0, i]);
let tile_y = part_y.load([i, pid.1]);
tile_z = mma(tile_x, tile_y, tile_z);
// TODO (hme): Inject continue.
continue;
}
z.store(tile_z);
}
#[cutile::entry()]
pub fn matvec<const BM: i32, const BK: i32, const K: i32>(
z: &mut Tensor<f32, { [BM] }>,
x: &Tensor<f32, { [-1, K] }>,
y: &Tensor<f32, { [K] }>,
) {
let part_x = x.partition(const_shape![BM, BK]);
let part_y = y.partition(const_shape![BK]);
let pid: (i32, i32, i32) = get_tile_block_id();
let mut tile_z = z.load().reshape(const_shape![BM, 1]);
for i in 0i32..(K / BK) {
let tile_x = part_x.load([pid.0, i]);
let tile_y = part_y.load([i]).reshape(const_shape![BK, 1]);
tile_z = mma(tile_x, tile_y, tile_z);
continue;
}
z.store(tile_z.reshape(const_shape![BM]));
}
#[cutile::entry()]
fn relu<const D: i32>(input_output: &mut Tensor<f32, { [D] }>) {
let zero_tile: Tile<f32, { [D] }> = constant(0.0f32, const_shape![D]);
let input = input_output.load();
input_output.store(max_tile(zero_tile, input));
}
}
use my_kernels::*;
// Simulate loading input data.
fn load_data<const RANK: usize>(
batch_size: [usize; RANK],
) -> impl DeviceOperation<Output = Tensor<f32>> {
api::randn(0.0, 1.0, batch_size)
}
#[tokio::main(flavor = "multi_thread", worker_threads = 16)]
async fn main() -> Result<(), Error> {
// Get device scheduling policies.
let num_devices = 4;
let devices = {
let mut r = vec![];
for _ in 0..num_devices {
// Pretend we have multiple devices...
r.push(global_policy(0)?);
}
r
};
let dim = 16;
let block_dim = 4;
let fully_connected_layer = [
block_dim.to_string(),
block_dim.to_string(),
block_dim.to_string(),
dim.to_string(),
];
let output_layer = [
block_dim.to_string(),
block_dim.to_string(),
dim.to_string(),
];
let w0 = api::randn(0.0f32, 1.0, [dim, dim]); // impl DeviceOperation
let w1 = api::randn(0.0f32, 1.0, [dim]); // impl DeviceOperation
let w = zip!(w0.arc(), w1.arc()).schedule(&devices[0])?.await?;
let mut joins = vec![];
for i in 1..num_devices {
let w_copy = tokio::spawn(zip!(copy(&w.0).arc(), copy(&w.1).arc()).schedule(&devices[i])?);
joins.push(w_copy);
}
let mut model_weights = vec![w];
for join in joins {
model_weights.push(join.await.unwrap()?);
}
// Asynchronously compute forward pass for each batch of data on each device.
let mut futures: Vec<
JoinHandle<Result<Partition<Tensor<f32>>, cuda_async::error::DeviceError>>,
> = vec![];
for i in 0..num_devices {
let w = &model_weights[i];
let (w0, w1) = (w.0.clone(), w.1.clone());
let data = load_data([dim, dim]).arc();
let out0 = api::zeros::<2, f32>([dim, dim]).partition([block_dim, block_dim]);
let (out0, _, _) = gemm_async(out0, data, value(w0))
.generics(fully_connected_layer.to_vec())
.unzip();
let out1 = api::zeros::<1, f32>([dim]).partition([block_dim]);
let (out1, _, _) = matvec_async(out1, out0.unpartition().arc(), value(w1))
.generics(output_layer.to_vec())
.unzip();
let (out1,) = relu_async(out1).unzip();
futures.push(tokio::spawn(out1.schedule(&devices[i])?));
}
// Wait on results.
let mut outputs: Vec<Tensor<f32>> = vec![];
for future in futures.into_iter() {
let tensor = future.await.unwrap()?.unpartition();
outputs.push(tensor);
}
for output in outputs {
println!("{:?}", output.to_host_vec().await?);
}
Ok(())
}