0

我在 PyTorch 源代码中设置了一个自制的跟踪器。当我尝试构建以前版本的代码时,遇到以下错误:

(编辑输出,它与make VERBOSE=1

[ 47%] Linking CXX shared library ../lib/libcaffe2.so
cd /opt/pytorch/build/caffe2 && /usr/bin/cmake -E cmake_link_script CMakeFiles/caffe2.dir/link.txt --verbose=1
/usr/bin/c++  -fPIC  -Wno-deprecated -fvisibility-inlines-hidden -fopenmp -DUSE_FBGEMM -O2 -fPIC -Wno-narrowing -fPIC -Wall -Wextra -Wno-missing-field-initializers -Wno-type-limits -Wno-array-bounds -Wno-unknown-pragmas -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wno-unused-function -Wno-unused-result -Wno-strict-overflow -Wno-strict-aliasing -Wno-error=deprecated-declarations -Wno-error=pedantic 

[...]

,--whole-archive,/opt/pytorch/build/lib/libonnx.a -Wl,--no-whole-archive ../lib/libonnx_proto.a ../lib/libprotobuf.a -pthread -Wl,-rpath,/opt/pytorch/build/lib:/opt/conda/envs/build/lib:/usr/local/cuda/lib64:
/usr/bin/ld: CMakeFiles/caffe2.dir/__/aten/src/ATen/native/cpu/Activation.cpp.AVX2.cpp.o: relocation R_X86_64_PC32 against undefined symbol `_ZN2at6vec25612_GLOBAL__N_118SOURCE_CODE_TRACERE' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: final link failed: Bad value
collect2: error: ld returned 1 exit status
caffe2/CMakeFiles/caffe2.dir/build.make:17251: recipe for target 'lib/libcaffe2.so' failed
make[3]: *** [lib/libcaffe2.so] Error 1
make[3]: Leaving directory '/opt/pytorch/build'
CMakeFiles/Makefile2:3900: recipe for target 'caffe2/CMakeFiles/caffe2.dir/all' failed
make[2]: *** [caffe2/CMakeFiles/caffe2.dir/all] Error 2
make[2]: Leaving directory '/opt/pytorch/build'
Makefile:138: recipe for target 'all' failed
make[1]: *** [all] Error 2
make[1]: Leaving directory '/opt/pytorch/build'
Makefile:4: recipe for target 'all' failed
make: *** [all] Error 2

我不确定我是否错过了跟踪器类导入的步骤,或者问题可能与所需的共享库(libcaffe2.so?)相关联(例如:Relocation R_X86_64_PC32 against undefined symbol can not be used when making共享对象;使用 -fPIC 重新编译)?

要使用示踪剂,我这样做:

#include "ATen/cpu/vec256/SourceCodeTracer.h"

namespace at {
namespace vec256 {
namespace {

#ifdef __AVX2__

struct Vec256i {
protected:
  __m256i values;

  static inline __m256i invert(const __m256i& v) {
    // some code
    extern SourceCodeTracer SOURCE_CODE_TRACER;
    SOURCE_CODE_TRACER.trace("vec256_int.h __m256i invert()");
    return something;
  }

类 cpp 文件:

#include "SourceCodeTracer.h"

SourceCodeTracer* SourceCodeTracer::_singleton = NULL;
extern SourceCodeTracer SOURCE_CODE_TRACER = SourceCodeTracer::getInstance();

SourceCodeTracer::~SourceCodeTracer() {
        // TODO: Write into
        std::cout << "Destructing object" << std::endl;
        ofstream traceOutputFile;
        try
        {
                std::cout << "Writing to " << _filePath << std::endl;
                traceOutputFile.open(_filePath);
                traceOutputFile << "Run at TODO PUT TIME" << std::endl;
                for (auto it = _map.begin(); it != _map.end(); ++it) {
                        traceOutputFile << it->first << " => " << it->second << '\n';
                }
        }
        catch (const std::exception& e)
        {
                std::cout << "Got error while trying to write trace file:" << e.what() << std::endl;
        }
        traceOutputFile.close();
}

SourceCodeTracer & SourceCodeTracer::getInstance() {
        if (SourceCodeTracer::_singleton == nullptr) {
                std::cout << "Constructing object" << std::endl;
                SourceCodeTracer::_singleton = new SourceCodeTracer();
        }
        return *SourceCodeTracer::_singleton;
}

void SourceCodeTracer::trace(const string & tr) {
        _map[tr] = _map[tr] + 1;
}

SourceCodeTracer::SourceCodeTracer() {
        _filePath = "trace.txt"; //get_env_var("TRACER_OUTPUT_FILE_PATH");
        std::cout << "Got environment value for TRACER_OUTPUT_FILE_PATH of " << _filePath << std::endl;
        if (_filePath == "") {
                // Warn that no environment variable was set
                std::cout << "No value for TRACER_OUTPUT_FILE_PATH set !" << std::endl;
                exit(1);
        }
        // Assert path is writable
}

// https://stackoverflow.com/q/631664/9876427
std::string SourceCodeTracer::get_env_var(std::string const & key) const {
        char * val;
        val = std::getenv(key.c_str());
        std::string retval = "";
        if (val != NULL) {
                retval = val;
        }
        return retval;
}

头文件:

#include <iostream>
#include <string>
#include <fstream>
#include <list>
#include <map>
#include <memory>
#include <iomanip>
#include <ios>
#include <cstddef>
#include <cstdlib>


#ifndef __SOURCE_CODE_TRACER_H__
#define __SOURCE_CODE_TRACER_H__

using namespace std;
class SourceCodeTracer {
        public:
        ~SourceCodeTracer();

        static SourceCodeTracer& getInstance();
        void trace(const string& tr);
private:
        SourceCodeTracer();
        // https://stackoverflow.com/q/631664/9876427
        std::string get_env_var(std::string const & key) const;
        static SourceCodeTracer* _singleton;
        // Traceur() = delete;
        std::map<string, long> _map;
        string _filePath;
};

#endif // __SOURCE_CODE_TRACER_H__
4

0 回答 0