MCPcopy Create free account
hub / github.com/NVIDIA/TensorRT / convertAndCopy

Method convertAndCopy

plugin/common/bertCommon.h:331–388  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

329 WeightsWithOwnership operator=(WeightsWithOwnership const&&) = delete;
330
331 void convertAndCopy(nvinfer1::Weights const& src, nvinfer1::DataType type)
332 {
333 this->type = type;
334 this->count = src.count;
335
336 if (type == nvinfer1::DataType::kFLOAT)
337 {
338 auto destBuf = new float[src.count];
339 this->values = destBuf;
340
341 if (src.type == nvinfer1::DataType::kFLOAT)
342 {
343 BERT_DEBUG_MSG("Float Weights(Host) => Float Array(Host)");
344 std::copy_n(static_cast<float const*>(src.values), src.count, destBuf);
345 }
346 else
347 {
348 PLUGIN_ASSERT(src.type == nvinfer1::DataType::kHALF);
349
350 BERT_DEBUG_MSG("Half Weights(Host) => Float Array(Host)");
351 auto const s = static_cast<half const*>(src.values);
352 auto d = static_cast<float*>(const_cast<void*>(this->values));
353
354 for (auto it = 0; it < src.count; it++)
355 {
356 d[it] = __half2float(s[it]);
357 }
358 }
359 }
360 else if (type == nvinfer1::DataType::kHALF)
361 {
362 auto destBuf = new half[src.count];
363 this->values = destBuf;
364
365 if (src.type == nvinfer1::DataType::kHALF)
366 {
367 BERT_DEBUG_MSG("Half Weights(Host) => Half Array(Host)");
368 std::copy_n(static_cast<half const*>(src.values), src.count, destBuf);
369 }
370 else
371 {
372 PLUGIN_ASSERT(src.type == nvinfer1::DataType::kFLOAT);
373
374 BERT_DEBUG_MSG("Float Weights(Host) => Half Array(Host)");
375 auto const s = static_cast<float const*>(src.values);
376 auto d = static_cast<half*>(const_cast<void*>(this->values));
377
378 for (auto it = 0; it < src.count; it++)
379 {
380 d[it] = __float2half(s[it]);
381 }
382 }
383 }
384 else
385 {
386 throw std::runtime_error("Unsupported DataType specified for plugin.");
387 }
388 }

Calls 1

getWeightsSizeFunction · 0.85

Tested by

no test coverage detected