MCPcopy Create free account
hub / github.com/NVIDIA/FasterTransformer / loadNpy

Method loadNpy

src/fastertransformer/utils/Tensor.cc:135–162  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

133}
134
135Tensor Tensor::loadNpy(const std::string& npy_file, const MemoryType where)
136{
137 DataType type;
138 std::vector<size_t> shape;
139
140 FILE* f_ptr = fopen(npy_file.c_str(), "rb");
141 if (f_ptr == nullptr) {
142 throw std::runtime_error("Could not open file " + npy_file);
143 }
144 uint32_t header_len, start_data;
145 parseNpyIntro(f_ptr, header_len, start_data);
146 parseNpyHeader(f_ptr, header_len, type, shape);
147
148 const size_t size = std::accumulate(shape.begin(), shape.end(), 1, std::multiplies<size_t>());
149 void* data_cpu = malloc(size * Tensor::getTypeSize(type));
150 void* data = data_cpu;
151
152 size_t n_elems = fread(data_cpu, Tensor::getTypeSize(type), size, f_ptr);
153 FT_CHECK_WITH_INFO(n_elems == size, "reading tensor failed");
154 if (where == MEMORY_GPU) {
155 cudaMalloc(&data, size * Tensor::getTypeSize(type));
156 cudaMemcpy(data, data_cpu, size * Tensor::getTypeSize(type), cudaMemcpyHostToDevice);
157 free(data_cpu);
158 }
159
160 fclose(f_ptr);
161 return Tensor(where, type, shape, data);
162}
163
164size_t Tensor::size() const
165{

Callers

nothing calls this directly

Calls 3

TensorClass · 0.70
beginMethod · 0.45
endMethod · 0.45

Tested by

no test coverage detected