File: custom_ops.cpp

package info (click to toggle)
pytorch-cuda 2.6.0%2Bdfsg-7
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid, trixie
  • size: 161,620 kB
  • sloc: python: 1,278,832; cpp: 900,322; ansic: 82,710; asm: 7,754; java: 3,363; sh: 2,811; javascript: 2,443; makefile: 597; ruby: 195; xml: 84; objc: 68
file content (378 lines) | stat: -rw-r--r-- 12,520 bytes parent folder | download | duplicates (3)
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#include <torch/csrc/api/include/torch/types.h>  // @manual=fbcode//caffe2:libtorch

#include <cstdint>
#include <iostream>
#include <string>

namespace at {

Tensor custom_add_impl(Tensor t1, Tensor t2) {
  return t1 + t2;
}

Tensor fn_with_all_inputs_impl(
    const Tensor& tensor,
    const c10::List<Tensor>& tensors,
    const c10::List<std::optional<Tensor>>& optional_tensors,
    const bool b8,
    const c10::List<bool>& b8s,
    const int64_t i64,
    const c10::List<int64_t>& i64s,
    const int64_t& symint,
    const IntArrayRef symints,
    const double f64,
    const c10::List<double>& f64s,
    const at::Scalar& scalar,
    at::ArrayRef<at::Scalar> scalars,
    const std::string& string,
    const std::vector<std::string>& strings,
    // const c10::ScalarType& dtype,
    // const MemoryFormat& memory_format,
    // const Layout& layout,
    const Device& device,
    // optional
    const std::optional<Tensor>& o_tensor,
    const std::optional<c10::List<Tensor>>& o_tensors,
    const std::optional<bool>& o_b8,
    const std::optional<c10::List<bool>>& o_b8s,
    const std::optional<int64_t>& o_i64,
    const std::optional<c10::List<int64_t>>& o_i64s,
    const std::optional<int64_t>& o_symint,
    const std::optional<IntArrayRef>& o_symints,
    const std::optional<double>& o_f64,
    const std::optional<c10::List<double>>& o_f64s,
    const std::optional<at::Scalar>& o_scalar,
    const std::optional<at::ArrayRef<at::Scalar>>& o_scalars,
    const std::optional<std::string>& o_string,
    const std::optional<std::vector<std::string>>& o_strings,
    // const std::optional<c10::ScalarType>& o_dtype,
    // const std::optional<MemoryFormat>& o_memory_format,
    // const std::optional<Layout>& o_layout,
    const std::optional<Device>& o_device) {
  std::cout << "tensor shape: " << tensor.sizes() << std::endl;

  std::cout << "tensors shape: ";
  for (auto t : tensors) {
    std::cout << t.get().toTensor().sizes() << ", ";
  }
  std::cout << std::endl;

  std::cout << "optional tensors shape: ";
  for (auto t : optional_tensors) {
    if (t.get().toOptional<Tensor>().has_value()) {
      std::cout << t.get().toTensor().sizes() << ", ";
    } else {
      std::cout << "None, ";
    }
  }
  std::cout << std::endl;

  std::cout << "b8 " << c10::IValue(b8) << std::endl;
  std::cout << "b8s " << c10::IValue(b8s) << std::endl;
  std::cout << "i64 " << c10::IValue(i64) << std::endl;
  std::cout << "i64s " << c10::IValue(i64s) << std::endl;
  std::cout << "symint " << c10::IValue(symint) << std::endl;
  std::cout << "symints " << c10::IValue(symints) << std::endl;
  std::cout << "f64 " << c10::IValue(f64) << std::endl;
  std::cout << "f64s " << c10::IValue(f64s) << std::endl;
  std::cout << "scalar " << c10::IValue(scalar) << std::endl;
  std::cout << "scalars " << c10::IValue(scalars) << std::endl;
  std::cout << "string " << c10::IValue(string) << std::endl;
  std::cout << "strings " << c10::IValue(strings) << std::endl;
  // std::cout << "dtype " << c10::IValue(dtype) << std::endl;
  // std::cout << "memory_format " << c10::IValue(memory_format) << std::endl;
  // std::cout << "layout " << c10::IValue(layout) << std::endl;
  std::cout << "device " << c10::IValue(device) << std::endl;

  std::cout << "o_tensor "
            << (o_tensor.has_value() ? c10::IValue(o_tensor.value().sizes())
                                     : "None")
            << std::endl;

  std::cout << "o_tensors shape: ";
  if (o_tensors.has_value()) {
    for (auto t : o_tensors.value()) {
      std::cout << t.get().toTensor().sizes() << ", ";
    }
  } else {
    std::cout << "None";
  }
  std::cout << std::endl;

  std::cout << "o_b8 "
            << (o_b8.has_value() ? c10::IValue(o_b8.value()) : "None")
            << std::endl;
  std::cout << "o_b8s "
            << (o_b8s.has_value() ? c10::IValue(o_b8s.value()) : "None")
            << std::endl;
  std::cout << "o_i64 "
            << (o_i64.has_value() ? c10::IValue(o_i64.value()) : "None")
            << std::endl;
  std::cout << "o_i64s "
            << (o_i64s.has_value() ? c10::IValue(o_i64s.value()) : "None")
            << std::endl;
  std::cout << "o_symint "
            << (o_symint.has_value() ? c10::IValue(o_symint.value()) : "None")
            << std::endl;
  std::cout << "o_symints "
            << (o_symints.has_value() ? c10::IValue(o_symints.value()) : "None")
            << std::endl;
  std::cout << "o_f64 "
            << (o_f64.has_value() ? c10::IValue(o_f64.value()) : "None")
            << std::endl;
  std::cout << "o_f64s "
            << (o_f64s.has_value() ? c10::IValue(o_f64s.value()) : "None")
            << std::endl;
  std::cout << "o_scalar "
            << (o_scalar.has_value() ? c10::IValue(o_scalar.value()) : "None")
            << std::endl;
  std::cout << "o_scalars "
            << (o_scalars.has_value() ? c10::IValue(o_scalars.value()) : "None")
            << std::endl;
  std::cout << "o_string "
            << (o_string.has_value() ? c10::IValue(o_string.value()) : "None")
            << std::endl;
  std::cout << "o_strings "
            << (o_strings.has_value() ? c10::IValue(o_strings.value()) : "None")
            << std::endl;
  // std::cout << "o_dtype "
  //           << (o_dtype.has_value() ? c10::IValue(o_dtype.value()) : "None")
  //           << std::endl;
  // std::cout << "o_memory_format "
  //           << (o_memory_format.has_value()
  //                   ? c10::IValue(o_memory_format.value())
  //                   : "None")
  //           << std::endl;
  // std::cout << "o_layout "
  //           << (o_layout.has_value() ? c10::IValue(o_layout.value()) : "None")
  //           << std::endl;
  std::cout << "o_device "
            << (o_device.has_value() ? c10::IValue(o_device.value()) : "None")
            << std::endl;

  int64_t int_hash = 0;
  int_hash ^= i64;
  for (auto i : i64s) {
    int_hash ^= i;
  }
  if (o_i64.has_value()) {
    int_hash ^= o_i64.value();
  }
  if (o_i64s.has_value()) {
    for (auto i : o_i64s.value()) {
      int_hash ^= i;
    }
  }

  int_hash ^= symint;
  for (auto i : symints) {
    int_hash ^= i;
  }
  if (o_symint.has_value()) {
    int_hash ^= o_symint.value();
  }
  if (o_symints.has_value()) {
    for (auto i : o_symints.value()) {
      int_hash ^= i;
    }
  }

  return tensor + int_hash;
}

Tensor fn_with_default_input_impl(const Tensor& tensor, const int64_t i64) {
  return tensor + i64;
}

std::tuple<Tensor, Tensor> fn_with_tuple_output_impl(
    const Tensor& tensor,
    const int64_t i64) {
  return {tensor + i64, tensor - i64};
}

std::vector<Tensor> fn_with_list_output_impl(
    TensorList tensors,
    const int64_t i64) {
  std::vector<Tensor> outputs;
  for (auto& t : tensors) {
    outputs.emplace_back(t + i64);
  }
  return outputs;
}

std::tuple<Tensor, std::vector<Tensor>> fn_with_mix_outputs_impl(
    const Tensor& tensor,
    TensorList tensors) {
  std::vector<Tensor> outputs;
  for (auto& t : tensors) {
    outputs.emplace_back(t + 2);
  }
  return {tensor + 1, outputs};
}

std::tuple<Tensor, Tensor> fn_with_input_mutation_impl(
    Tensor& t0,
    const Tensor& t1,
    Tensor& t2) {
  t0.add_(1);
  t2.sub_(1);
  return {t1 + 1, t1 + 2};
}

void fn_out_variant_without_return_impl(
    const Tensor& x,
    Tensor& out) {
  out.add_(x);
}

// NOLINTBEGIN(clang-diagnostic-unused-parameter)
Tensor fn_with_all_inputs_meta(
    const Tensor& tensor,
    const c10::List<Tensor>& tensors,
    const c10::List<std::optional<Tensor>>& optional_tensors,
    const bool b8,
    const c10::List<bool>& b8s,
    const int64_t i64,
    const c10::List<int64_t>& i64s,
    const c10::SymInt& symint,
    c10::SymIntArrayRef symints,
    const double f64,
    const c10::List<double>& f64s,
    const at::Scalar& scalar,
    at::ArrayRef<at::Scalar> scalars,
    const std::string& string,
    const std::vector<std::string>& strings,
    // const c10::ScalarType& dtype,
    // const MemoryFormat& memory_format,
    // const Layout& layout,
    const Device& device,
    // optional
    const std::optional<Tensor>& o_tensor,
    const std::optional<c10::List<Tensor>>& o_tensors,
    const std::optional<bool>& o_b8,
    const std::optional<c10::List<bool>>& o_b8s,
    const std::optional<int64_t>& o_i64,
    const std::optional<c10::List<int64_t>>& o_i64s,
    const std::optional<c10::SymInt>& o_symint,
    at::OptionalSymIntArrayRef o_symints,
    const std::optional<double>& o_f64,
    const std::optional<c10::List<double>>& o_f64s,
    const std::optional<at::Scalar>& o_scalar,
    const std::optional<at::ArrayRef<at::Scalar>>& o_scalars,
    const std::optional<std::string>& o_string,
    const std::optional<std::vector<std::string>>& o_strings,
    // const std::optional<c10::ScalarType>& o_dtype,
    // const std::optional<MemoryFormat>& o_memory_format,
    // const std::optional<Layout>& o_layout,
    const std::optional<Device>& o_device) {
  return tensor;
}

Tensor fn_with_default_input_meta(const Tensor& tensor, const int64_t i64) {
  return tensor.clone();
}

std::tuple<Tensor, Tensor> fn_with_tuple_output_meta(
    const Tensor& tensor,
    const int64_t i64) {
  return {tensor.clone(), tensor.clone()};
}

std::vector<Tensor> fn_with_list_output_meta(
    TensorList tensors,
    const int64_t i64) {
  std::vector<Tensor> outputs;
  for (auto& t : tensors) {
    outputs.push_back(t.clone());
  }
  return outputs;
}

std::tuple<Tensor, std::vector<Tensor>> fn_with_mix_outputs_meta(
    const Tensor& tensor,
    TensorList tensors) {
  std::vector<Tensor> outputs;
  for (auto& t : tensors) {
    outputs.push_back(t.clone());
  }
  return {tensor.clone(), outputs};
}

std::tuple<Tensor, Tensor> fn_with_input_mutation_meta(
    Tensor& t0,
    const Tensor& t1,
    Tensor& t2) {
  return {t1.clone(), t1.clone()};
}

void fn_out_variant_without_return_meta(
    const Tensor& x,
    Tensor& out) {
}

} // namespace at

TORCH_LIBRARY(aoti_custom_ops, m) {
  m.def("custom_add(Tensor t1, Tensor t2) -> Tensor");
  m.def(
      "fn_with_all_inputs(Tensor tensor, "
      "Tensor[] tensors, "
      "Tensor?[] optional_tensors, "
      "bool b8, bool[] b8s, "
      "int i64, int[] i64s, "
      "SymInt symint, SymInt[] symints, "
      "float f64, float[] f64s, "
      "Scalar scalar, Scalar[] scalars, "
      "str string, str[] strings, "
      // "ScalarType dtype, "
      // "MemoryFormat memory_format, "
      // "Layout layout, "
      "Device device, "
      "*, "
      "Tensor? o_tensor, Tensor[]? o_tensors, "
      "bool? o_b8, bool[]? o_b8s, "
      "int? o_i64, int[]? o_i64s, "
      "SymInt? o_symint, SymInt[]? o_symints, "
      "float? o_f64, float[]? o_f64s, "
      "Scalar? o_scalar, Scalar[]? o_scalars, "
      "str? o_string, str[]? o_strings, "
      // "ScalarType? o_dtype, "
      // "MemoryFormat? o_memory_format, "
      // "Layout? o_layout, "
      "Device? o_device) -> Tensor");

  m.def("fn_with_default_input(Tensor t, int i=3) -> Tensor");

  m.def("fn_with_tuple_output(Tensor t, int i) -> (Tensor, Tensor)");

  m.def("fn_with_list_output(Tensor[] tensors, int i) -> Tensor[]");

  m.def(
      "fn_with_mix_outputs(Tensor t, Tensor[] tensors) -> (Tensor, Tensor[])");

  m.def(
      "fn_with_input_mutation(Tensor(a!) t0, Tensor t1, Tensor(b!) t2) -> (Tensor, Tensor)");

  m.def("fn_out_variant_without_return(Tensor x, Tensor(a!) out) -> ()");
}

TORCH_LIBRARY_IMPL(aoti_custom_ops, CompositeExplicitAutograd, m) {
  m.impl("custom_add", at::custom_add_impl);
  m.impl("fn_with_all_inputs", at::fn_with_all_inputs_impl);
  m.impl("fn_with_default_input", at::fn_with_default_input_impl);
  m.impl("fn_with_tuple_output", at::fn_with_tuple_output_impl);
  m.impl("fn_with_list_output", at::fn_with_list_output_impl);
  m.impl("fn_with_mix_outputs", at::fn_with_mix_outputs_impl);
  m.impl("fn_with_input_mutation", at::fn_with_input_mutation_impl);
  m.impl("fn_out_variant_without_return", at::fn_out_variant_without_return_impl);
}

TORCH_LIBRARY_IMPL(aoti_custom_ops, Meta, m) {
  m.impl("fn_with_all_inputs", at::fn_with_all_inputs_meta);
  m.impl("fn_with_default_input", at::fn_with_default_input_meta);
  m.impl("fn_with_tuple_output", at::fn_with_tuple_output_meta);
  m.impl("fn_with_list_output", at::fn_with_list_output_meta);
  m.impl("fn_with_mix_outputs", at::fn_with_mix_outputs_meta);
  m.impl("fn_with_input_mutation", at::fn_with_input_mutation_meta);
  m.impl("fn_out_variant_without_return", at::fn_out_variant_without_return_meta);
}