|
| 1 | +// Copyright (c) .NET Foundation and Contributors. All Rights Reserved. See LICENSE in the project root for license information. |
| 2 | +#include "THSExport.h" |
| 3 | + |
| 4 | +// NOTE: In LibTorch C++ API, ExportedProgram models (.pt2 files) are loaded using torch::jit::load() |
| 5 | +// The .pt2 format is compatible with the TorchScript loading infrastructure |
| 6 | + |
| 7 | +ExportedProgramModule THSExport_load(const char* filename, int64_t device, int64_t index) |
| 8 | +{ |
| 9 | + c10::DeviceType dev = c10::kCPU; |
| 10 | + if (device == 1) |
| 11 | + dev = c10::kCUDA; |
| 12 | + if (device == 13) |
| 13 | + dev = c10::kMPS; |
| 14 | + |
| 15 | + CATCH( |
| 16 | + // Load .pt2 file using torch::jit::load |
| 17 | + // This works because ExportedProgram models are serialized in a JIT-compatible format |
| 18 | + auto res = torch::jit::load(filename, torch::Device(dev, index)); |
| 19 | + auto copy = new torch::jit::Module(res); |
| 20 | + return new std::shared_ptr<torch::jit::Module>(copy); |
| 21 | + ); |
| 22 | + |
| 23 | + return nullptr; |
| 24 | +} |
| 25 | + |
| 26 | +void THSExport_Module_dispose(const ExportedProgramModule module) |
| 27 | +{ |
| 28 | + delete module; |
| 29 | +} |
| 30 | + |
| 31 | +void THSExport_Module_forward( |
| 32 | + const ExportedProgramModule module, |
| 33 | + const TensorOrScalar* tensorPtrs, |
| 34 | + const int length, |
| 35 | + TensorOrScalar* (*allocator)(int32_t idx, size_t length), |
| 36 | + int8_t* typeCode, |
| 37 | + int32_t idx) |
| 38 | +{ |
| 39 | + *typeCode = 0; |
| 40 | + |
| 41 | + CATCH( |
| 42 | + // Execute the forward method |
| 43 | + auto result = (*module)->forward(toIValue(tensorPtrs, length)); |
| 44 | + ReturnHelper(result, allocator, typeCode, &idx); |
| 45 | + ) |
| 46 | +} |
| 47 | + |
| 48 | +int THSExport_Module_is_training(ExportedProgramModule module) |
| 49 | +{ |
| 50 | + // ExportedPrograms are always in eval mode, but we check the underlying module |
| 51 | + return (*module)->is_training(); |
| 52 | +} |
| 53 | + |
| 54 | +void THSExport_Module_train(ExportedProgramModule module, bool on) |
| 55 | +{ |
| 56 | + // ExportedPrograms should remain in eval mode, but we allow this for compatibility |
| 57 | + (*module)->train(on); |
| 58 | +} |
| 59 | + |
| 60 | +void THSExport_Module_eval(ExportedProgramModule module) |
| 61 | +{ |
| 62 | + (*module)->eval(); |
| 63 | +} |
| 64 | + |
| 65 | +void THSExport_Module_to_device_dtype(ExportedProgramModule module, int8_t dtype, int64_t device, int64_t index) |
| 66 | +{ |
| 67 | + c10::DeviceType dev = c10::kCPU; |
| 68 | + if (device == 1) |
| 69 | + dev = c10::kCUDA; |
| 70 | + if (device == 13) |
| 71 | + dev = c10::kMPS; |
| 72 | + |
| 73 | + CATCH( |
| 74 | + (*module)->to(torch::Device(dev, index), (at::ScalarType)dtype); |
| 75 | + ); |
| 76 | +} |
| 77 | + |
| 78 | +void THSExport_Module_to_device(ExportedProgramModule module, int64_t device, int64_t index) |
| 79 | +{ |
| 80 | + c10::DeviceType dev = c10::kCPU; |
| 81 | + if (device == 1) |
| 82 | + dev = c10::kCUDA; |
| 83 | + if (device == 13) |
| 84 | + dev = c10::kMPS; |
| 85 | + |
| 86 | + CATCH( |
| 87 | + (*module)->to(torch::Device(dev, index)); |
| 88 | + ); |
| 89 | +} |
| 90 | + |
| 91 | +void THSExport_Module_to_dtype(ExportedProgramModule module, int8_t dtype) |
| 92 | +{ |
| 93 | + CATCH( |
| 94 | + (*module)->to((at::ScalarType)dtype); |
| 95 | + ); |
| 96 | +} |
| 97 | + |
| 98 | +void THSExport_Module_parameters(const ExportedProgramModule module, Tensor* (*allocator)(size_t length)) |
| 99 | +{ |
| 100 | + auto parameters = (*module)->parameters(); |
| 101 | + Tensor* result = allocator(parameters.size()); |
| 102 | + |
| 103 | + int i = 0; |
| 104 | + for (auto parameter : parameters) |
| 105 | + result[i++] = new torch::Tensor(parameter); |
| 106 | +} |
| 107 | + |
| 108 | +void THSExport_Module_named_parameters( |
| 109 | + const ExportedProgramModule module, |
| 110 | + Tensor* (*allocator)(size_t length), |
| 111 | + const char** (*allocator2)(size_t length)) |
| 112 | +{ |
| 113 | + auto parameters = (*module)->named_parameters(); |
| 114 | + Tensor* result = allocator(parameters.size()); |
| 115 | + const char** names = allocator2(parameters.size()); |
| 116 | + |
| 117 | + int i = 0; |
| 118 | + for (const auto& parameter : parameters) { |
| 119 | + result[i] = new torch::Tensor(parameter.value); |
| 120 | + names[i] = make_sharable_string(parameter.name); |
| 121 | + i++; |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +void THSExport_Module_named_buffers( |
| 126 | + const ExportedProgramModule module, |
| 127 | + Tensor* (*allocator)(size_t length), |
| 128 | + const char** (*allocator2)(size_t length)) |
| 129 | +{ |
| 130 | + auto buffers = (*module)->named_buffers(); |
| 131 | + Tensor* result = allocator(buffers.size()); |
| 132 | + const char** names = allocator2(buffers.size()); |
| 133 | + |
| 134 | + int i = 0; |
| 135 | + for (const auto& buffer : buffers) { |
| 136 | + result[i] = new torch::Tensor(buffer.value); |
| 137 | + names[i] = make_sharable_string(buffer.name); |
| 138 | + i++; |
| 139 | + } |
| 140 | +} |
0 commit comments