From 6836415e7adc1337ce5e83947c463126392bf24a Mon Sep 17 00:00:00 2001 From: Ivan Razumov Date: Thu, 20 Apr 2023 10:25:30 +0200 Subject: [PATCH] Remove PhysicsTools/MXNet (cms-sw/cmssw#30432) --- PhysicsTools/MXNet/BuildFile.xml | 5 - PhysicsTools/MXNet/interface/Predictor.h | 88 ----------- PhysicsTools/MXNet/src/Predictor.cc | 145 ------------------ PhysicsTools/MXNet/test/BuildFile.xml | 8 - .../MXNet/test/data/testmxnet-0000.params | Bin 480 -> 0 bytes .../MXNet/test/data/testmxnet-symbol.json | 79 ---------- .../MXNet/test/testMXNetCppPredictor.cc | 52 ------- PhysicsTools/MXNet/test/testRunner.cpp | 1 - PhysicsTools/MXNet/test/test_mxnet.py | 5 - 9 files changed, 383 deletions(-) delete mode 100644 PhysicsTools/MXNet/BuildFile.xml delete mode 100644 PhysicsTools/MXNet/interface/Predictor.h delete mode 100644 PhysicsTools/MXNet/src/Predictor.cc delete mode 100644 PhysicsTools/MXNet/test/BuildFile.xml delete mode 100644 PhysicsTools/MXNet/test/data/testmxnet-0000.params delete mode 100644 PhysicsTools/MXNet/test/data/testmxnet-symbol.json delete mode 100644 PhysicsTools/MXNet/test/testMXNetCppPredictor.cc delete mode 100644 PhysicsTools/MXNet/test/testRunner.cpp delete mode 100755 PhysicsTools/MXNet/test/test_mxnet.py diff --git a/PhysicsTools/MXNet/BuildFile.xml b/PhysicsTools/MXNet/BuildFile.xml deleted file mode 100644 index b8fec8dab6cff..0000000000000 --- a/PhysicsTools/MXNet/BuildFile.xml +++ /dev/null @@ -1,5 +0,0 @@ - - - - - diff --git a/PhysicsTools/MXNet/interface/Predictor.h b/PhysicsTools/MXNet/interface/Predictor.h deleted file mode 100644 index 0a87e32260c23..0000000000000 --- a/PhysicsTools/MXNet/interface/Predictor.h +++ /dev/null @@ -1,88 +0,0 @@ -/* - * MXNetCppPredictor.h - * - * Created on: Jul 19, 2018 - * Author: hqu - */ - -#ifndef PHYSICSTOOLS_MXNET_MXNETCPPPREDICTOR_H_ -#define PHYSICSTOOLS_MXNET_MXNETCPPPREDICTOR_H_ - -#include -#include -#include -#include - -#include "mxnet-cpp/MxNetCpp.h" - -namespace mxnet { - - namespace cpp { - - // note: Most of the objects in mxnet::cpp are effective just shared_ptr's - - // Simple class to hold MXNet model (symbol + params) - // designed to be sharable by multiple threads - class Block { - public: - Block(); - Block(const std::string& symbol_file, const std::string& param_file); - virtual ~Block(); - - const Symbol& symbol() const { return sym_; } - Symbol symbol(const std::string& output_node) const { return sym_.GetInternals()[output_node]; } - const std::map& arg_map() const { return arg_map_; } - const std::map& aux_map() const { return aux_map_; } - - private: - void load_parameters(const std::string& param_file); - - // symbol - Symbol sym_; - // argument arrays - std::map arg_map_; - // auxiliary arrays - std::map aux_map_; - }; - - // Simple helper class to run prediction - // this cannot be shared between threads - class Predictor { - public: - Predictor(); - Predictor(const Block& block); - Predictor(const Block& block, const std::string& output_node); - virtual ~Predictor(); - - // set input array shapes - void set_input_shapes(const std::vector& input_names, - const std::vector>& input_shapes); - - // run prediction - const std::vector& predict(const std::vector>& input_data); - - private: - static std::mutex mutex_; - - void bind_executor(); - - // context - static const Context context_; - // executor - std::unique_ptr exec_; - // symbol - Symbol sym_; - // argument arrays - std::map arg_map_; - // auxiliary arrays - std::map aux_map_; - // output of the prediction - std::vector pred_; - // names of the input nodes - std::vector input_names_; - }; - - } /* namespace cpp */ -} /* namespace mxnet */ - -#endif /* PHYSICSTOOLS_MXNET_MXNETCPPPREDICTOR_H_ */ diff --git a/PhysicsTools/MXNet/src/Predictor.cc b/PhysicsTools/MXNet/src/Predictor.cc deleted file mode 100644 index b99f4c076891c..0000000000000 --- a/PhysicsTools/MXNet/src/Predictor.cc +++ /dev/null @@ -1,145 +0,0 @@ -/* - * MXNetCppPredictor.cc - * - * Created on: Jul 19, 2018 - * Author: hqu - */ - -#include "PhysicsTools/MXNet/interface/Predictor.h" - -#include -#include - -#include "FWCore/Utilities/interface/Exception.h" - -namespace mxnet { - - namespace cpp { - - Block::Block() {} - - Block::Block(const std::string& symbol_file, const std::string& param_file) { - // load the symbol - sym_ = Symbol::Load(symbol_file); - // load the parameters - load_parameters(param_file); - } - - Block::~Block() {} - - void Block::load_parameters(const std::string& param_file) { - std::map paramters; - NDArray::Load(param_file, nullptr, ¶mters); - for (const auto& k : paramters) { - if (k.first.substr(0, 4) == "aux:") { - auto name = k.first.substr(4, k.first.size() - 4); - aux_map_[name] = k.second; - } - if (k.first.substr(0, 4) == "arg:") { - auto name = k.first.substr(4, k.first.size() - 4); - arg_map_[name] = k.second; - } - } - } - - std::mutex Predictor::mutex_; - const Context Predictor::context_ = Context(DeviceType::kCPU, 0); - - Predictor::Predictor() {} - - Predictor::Predictor(const Block& block) - : sym_(block.symbol()), arg_map_(block.arg_map()), aux_map_(block.aux_map()) {} - - Predictor::Predictor(const Block& block, const std::string& output_node) - : sym_(block.symbol(output_node)), arg_map_(block.arg_map()), aux_map_(block.aux_map()) {} - - Predictor::~Predictor() {} - - void Predictor::set_input_shapes(const std::vector& input_names, - const std::vector >& input_shapes) { - assert(input_names.size() == input_shapes.size()); - input_names_ = input_names; - // init the input NDArrays and add them to the arg_map - for (unsigned i = 0; i < input_names_.size(); ++i) { - const auto& name = input_names_[i]; - arg_map_.emplace(name, NDArray(input_shapes[i], context_, false)); - } - } - - const std::vector& Predictor::predict(const std::vector >& input_data) { - assert(input_names_.size() == input_data.size()); - - try { - // create the executor (if not done yet) - if (!exec_) { - bind_executor(); - } - assert(exec_); - // set the inputs - for (unsigned i = 0; i < input_names_.size(); ++i) { - const auto& name = input_names_[i]; - arg_map_[name].SyncCopyFromCPU(input_data[i]); - } - // run forward - exec_->Forward(false); - // copy the output to pred_ - exec_->outputs[0].SyncCopyToCPU(&pred_); - return pred_; - } catch (const dmlc::Error& e) { - throw cms::Exception("RuntimeError") << e.what() << MXGetLastError(); - } - } - - void Predictor::bind_executor() { - // acquire lock - std::lock_guard lock(mutex_); - - // infer shapes - const auto arg_name_list = sym_.ListArguments(); - std::vector > in_shapes, aux_shapes, out_shapes; - std::map > arg_shapes; - - for (const auto& arg_name : arg_name_list) { - auto iter = arg_map_.find(arg_name); - if (iter != arg_map_.end()) { - arg_shapes[arg_name] = iter->second.GetShape(); - } - } - sym_.InferShape(arg_shapes, &in_shapes, &aux_shapes, &out_shapes); - - // init argument arrays - std::vector arg_arrays; - for (size_t i = 0; i < in_shapes.size(); ++i) { - const auto& shape = in_shapes[i]; - const auto& arg_name = arg_name_list[i]; - auto iter_arg = arg_map_.find(arg_name); - if (iter_arg != arg_map_.end()) { - arg_arrays.push_back(iter_arg->second); - } else { - arg_arrays.push_back(NDArray(shape, context_, false)); - } - } - std::vector grad_arrays(arg_arrays.size()); - std::vector grad_reqs(arg_arrays.size(), kNullOp); - - // init auxiliary array - std::vector aux_arrays; - const auto aux_name_list = sym_.ListAuxiliaryStates(); - for (size_t i = 0; i < aux_shapes.size(); ++i) { - const auto& shape = aux_shapes[i]; - const auto& aux_name = aux_name_list[i]; - auto iter_aux = aux_map_.find(aux_name); - if (iter_aux != aux_map_.end()) { - aux_arrays.push_back(iter_aux->second); - } else { - aux_arrays.push_back(NDArray(shape, context_, false)); - } - } - - // bind executor - exec_ = std::make_unique(sym_, context_, arg_arrays, grad_arrays, grad_reqs, aux_arrays); - } - - } // namespace cpp - -} /* namespace mxnet */ diff --git a/PhysicsTools/MXNet/test/BuildFile.xml b/PhysicsTools/MXNet/test/BuildFile.xml deleted file mode 100644 index 793f02af45f59..0000000000000 --- a/PhysicsTools/MXNet/test/BuildFile.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - diff --git a/PhysicsTools/MXNet/test/data/testmxnet-0000.params b/PhysicsTools/MXNet/test/data/testmxnet-0000.params deleted file mode 100644 index 3c6a87622c52104480fc6c12b765af6374e00e3f..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 480 zcmWe)WIzEdP~OR3lYfGkOhC*G6=R3es9GBA85pRGVRnP;f!U2}C)6Q83Rx|7_aW4x zvtW8beuU}8ZXb312MRA(I154@l~|N+m6DoQoN5?fo|>7SQNoWYk(8NOj3I0g4-&>O I#sF#z07SM$t^fc4 diff --git a/PhysicsTools/MXNet/test/data/testmxnet-symbol.json b/PhysicsTools/MXNet/test/data/testmxnet-symbol.json deleted file mode 100644 index c423651756d8e..0000000000000 --- a/PhysicsTools/MXNet/test/data/testmxnet-symbol.json +++ /dev/null @@ -1,79 +0,0 @@ -{ - "nodes": [ - { - "op": "null", - "name": "data", - "inputs": [] - }, - { - "op": "null", - "name": "dense0_weight", - "attrs": { - "__dtype__": "0", - "__lr_mult__": "1.0", - "__shape__": "(7, 0)", - "__wd_mult__": "1.0" - }, - "inputs": [] - }, - { - "op": "null", - "name": "dense0_bias", - "attrs": { - "__dtype__": "0", - "__init__": "zeros", - "__lr_mult__": "1.0", - "__shape__": "(7,)", - "__wd_mult__": "1.0" - }, - "inputs": [] - }, - { - "op": "FullyConnected", - "name": "dense0_fwd", - "attrs": { - "flatten": "True", - "no_bias": "False", - "num_hidden": "7" - }, - "inputs": [[0, 0, 0], [1, 0, 0], [2, 0, 0]] - }, - { - "op": "null", - "name": "dense1_weight", - "attrs": { - "__dtype__": "0", - "__lr_mult__": "1.0", - "__shape__": "(3, 0)", - "__wd_mult__": "1.0" - }, - "inputs": [] - }, - { - "op": "null", - "name": "dense1_bias", - "attrs": { - "__dtype__": "0", - "__init__": "zeros", - "__lr_mult__": "1.0", - "__shape__": "(3,)", - "__wd_mult__": "1.0" - }, - "inputs": [] - }, - { - "op": "FullyConnected", - "name": "dense1_fwd", - "attrs": { - "flatten": "True", - "no_bias": "False", - "num_hidden": "3" - }, - "inputs": [[3, 0, 0], [4, 0, 0], [5, 0, 0]] - } - ], - "arg_nodes": [0, 1, 2, 4, 5], - "node_row_ptr": [0, 1, 2, 3, 4, 5, 6, 7], - "heads": [[6, 0, 0]], - "attrs": {"mxnet_version": ["int", 10200]} -} \ No newline at end of file diff --git a/PhysicsTools/MXNet/test/testMXNetCppPredictor.cc b/PhysicsTools/MXNet/test/testMXNetCppPredictor.cc deleted file mode 100644 index c8dd5545148e5..0000000000000 --- a/PhysicsTools/MXNet/test/testMXNetCppPredictor.cc +++ /dev/null @@ -1,52 +0,0 @@ -#include - -#include "PhysicsTools/MXNet/interface/Predictor.h" -#include "FWCore/ParameterSet/interface/FileInPath.h" - -using namespace mxnet::cpp; - -class testMXNetCppPredictor : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE(testMXNetCppPredictor); - CPPUNIT_TEST(checkAll); - CPPUNIT_TEST_SUITE_END(); - -public: - void checkAll(); -}; - -CPPUNIT_TEST_SUITE_REGISTRATION(testMXNetCppPredictor); - -void testMXNetCppPredictor::checkAll() { - std::string model_path = edm::FileInPath("PhysicsTools/MXNet/test/data/testmxnet-symbol.json").fullPath(); - std::string param_path = edm::FileInPath("PhysicsTools/MXNet/test/data/testmxnet-0000.params").fullPath(); - - // load model and params - Block *block = nullptr; - CPPUNIT_ASSERT_NO_THROW(block = new Block(model_path, param_path)); - CPPUNIT_ASSERT(block != nullptr); - - // create predictor - Predictor predictor(*block); - - // set input shape - std::vector input_names{"data"}; - std::vector> input_shapes{{1, 3}}; - CPPUNIT_ASSERT_NO_THROW(predictor.set_input_shapes(input_names, input_shapes)); - - // run predictor - std::vector> data{{ - 1, - 2, - 3, - }}; - std::vector outputs; - CPPUNIT_ASSERT_NO_THROW(outputs = predictor.predict(data)); - - // check outputs - CPPUNIT_ASSERT(outputs.size() == 3); - CPPUNIT_ASSERT(outputs.at(0) == 42); - CPPUNIT_ASSERT(outputs.at(1) == 42); - CPPUNIT_ASSERT(outputs.at(2) == 42); - - delete block; -} diff --git a/PhysicsTools/MXNet/test/testRunner.cpp b/PhysicsTools/MXNet/test/testRunner.cpp deleted file mode 100644 index 1482cf9a9ce85..0000000000000 --- a/PhysicsTools/MXNet/test/testRunner.cpp +++ /dev/null @@ -1 +0,0 @@ -#include diff --git a/PhysicsTools/MXNet/test/test_mxnet.py b/PhysicsTools/MXNet/test/test_mxnet.py deleted file mode 100755 index 16886f777a555..0000000000000 --- a/PhysicsTools/MXNet/test/test_mxnet.py +++ /dev/null @@ -1,5 +0,0 @@ -#!/usr/bin/env python3 -import mxnet as mx -a = mx.nd.ones((2, 3)) -b = a * 2 + 1 -b.asnumpy()