|
23 | 23 | #include <tuple> |
24 | 24 | #include <memory> |
25 | 25 |
|
| 26 | +#ifdef USE_TC |
| 27 | +#include <dlpack/dlpack.h> |
| 28 | +#include <tc/core/tensor.h> |
| 29 | +#include <tc/utils/compiler_options.h> |
| 30 | +#include <tc/core/compiler.h> |
| 31 | +#include <tc/core/utils/time.h> |
| 32 | +#include <tc/core/cuda/cuda_backend.h> |
| 33 | +#include <tc/core/cuda/cuda_tc_executor.h> |
| 34 | +#include <tc/core/cpu/cpu_backend.h> |
| 35 | +#include <tc/core/cpu/cpu_tc_executor.h> |
| 36 | +#include <tc/core/check.h> |
| 37 | +#include <tc/core/tc_executor.h> |
| 38 | +#endif // USE_TC |
| 39 | + |
26 | 40 | #include "singa/core/common.h" |
27 | 41 | #include "singa/core/device.h" |
28 | 42 | #include "singa/proto/core.pb.h" |
@@ -603,6 +617,85 @@ Tensor ConcatRows(const vector<Tensor> &in); |
603 | 617 | Tensor ConcatenateColumns(const vector<Tensor> &in); |
604 | 618 | /// Alias name for function ConcatenateColumns |
605 | 619 | Tensor ConcatColumns(const vector<Tensor> &in); |
| 620 | + |
| 621 | + |
| 622 | + |
| 623 | + |
| 624 | +#ifdef USE_TC |
| 625 | +/// tc integration start |
| 626 | +DLManagedTensor *toDLPack(const Tensor &src); |
| 627 | + |
| 628 | +inline std::vector<tc::DLTensorUPtr> |
| 629 | +makeDLTensors(const std::vector<Tensor> &tensors); |
| 630 | + |
| 631 | +template <typename Backend> |
| 632 | +std::unique_ptr<typename Backend::ExecutorType> |
| 633 | +compileTC(const std::string &tc, const std::string &entryPoint, |
| 634 | + const std::vector<Tensor> &inputs, |
| 635 | + const typename Backend::MappingOptionsType &options, |
| 636 | + const tc::CompilerOptions &compilerOptions = tc::CompilerOptions()); |
| 637 | + |
| 638 | +std::vector<tc::DLTensorUPtr> |
| 639 | +inferOutputTensorInfo(const std::string &tc, const std::string &entryPoint, |
| 640 | + const std::vector<Tensor> &inputs); |
| 641 | + |
| 642 | +std::vector<Tensor> prepareOutputs(const std::string &tc, |
| 643 | + const std::string &entryPoint, |
| 644 | + const std::vector<Tensor> &inputs); |
| 645 | + |
| 646 | +template <typename Executor> |
| 647 | +void runTC(const Executor &executor, const std::vector<Tensor> &inputs, |
| 648 | + std::vector<Tensor> &outputs); |
| 649 | + |
| 650 | +// makeDLConstTensors implementation |
| 651 | +inline std::vector<tc::DLConstTensorUPtr> |
| 652 | +makeDLConstTensors(const std::vector<Tensor> &tensors) { |
| 653 | + std::vector<tc::DLConstTensorUPtr> dlTensors; |
| 654 | + for (auto tensor : tensors) { |
| 655 | + auto dlMTensor = toDLPack(tensor); |
| 656 | + dlTensors.push_back(tc::makeDLConstTensor(&(dlMTensor->dl_tensor))); |
| 657 | + dlMTensor->deleter(dlMTensor); |
| 658 | + } |
| 659 | + return dlTensors; |
| 660 | +} |
| 661 | + |
| 662 | +// makeDLTensors implementation |
| 663 | +inline std::vector<tc::DLTensorUPtr> |
| 664 | +makeDLTensors(const std::vector<Tensor> &tensors) { |
| 665 | + std::vector<tc::DLTensorUPtr> dlTensors; |
| 666 | + for (auto tensor : tensors) { |
| 667 | + auto dlMTensor = toDLPack(tensor); |
| 668 | + dlTensors.push_back(tc::makeDLTensor(&(dlMTensor->dl_tensor))); |
| 669 | + dlMTensor->deleter(dlMTensor); |
| 670 | + } |
| 671 | + return dlTensors; |
| 672 | +} |
| 673 | + |
| 674 | +// compile implementation |
| 675 | +template <typename Backend> |
| 676 | +std::unique_ptr<typename Backend::ExecutorType> |
| 677 | +compileTC(const std::string &tc, const std::string &entryPoint, |
| 678 | + const std::vector<Tensor> &inputs, |
| 679 | + const typename Backend::MappingOptionsType &options, |
| 680 | + const tc::CompilerOptions &compilerOptions) { |
| 681 | + auto inputDLTensors = makeDLConstTensors(inputs); |
| 682 | + return tc::compile<Backend>(tc, entryPoint, extractRawPtrs(inputDLTensors), |
| 683 | + options, compilerOptions); |
| 684 | +} |
| 685 | + |
| 686 | +// run implementation |
| 687 | +template <typename Executor> |
| 688 | +void runTC(const Executor &executor, const std::vector<Tensor> &inputs, |
| 689 | + std::vector<Tensor> &outputs) { |
| 690 | + auto inputDLTensors = makeDLConstTensors(inputs); |
| 691 | + auto outputDLTensors = makeDLTensors(outputs); |
| 692 | + return executor.run(extractRawPtrs(inputDLTensors), |
| 693 | + extractRawPtrs(outputDLTensors)); |
| 694 | +} |
| 695 | + |
| 696 | +/// tc integration end |
| 697 | +#endif // USE_TC |
| 698 | + |
606 | 699 | } // namespace singa |
607 | 700 |
|
608 | 701 | #endif // SINGA_CORE_TENSOR_H_ |
0 commit comments