xref: /aosp_15_r20/external/pytorch/scripts/diagnose_protobuf.py (revision da0073e96a02ea20f0ac840b70461e3646d07c45)
1*da0073e9SAndroid Build Coastguard Worker## @package diagnose_protobuf
2*da0073e9SAndroid Build Coastguard Worker# Module scripts.diagnose_protobuf
3*da0073e9SAndroid Build Coastguard Worker"""Diagnoses the current protobuf situation.
4*da0073e9SAndroid Build Coastguard Worker
5*da0073e9SAndroid Build Coastguard WorkerProtocol buffer needs to be properly installed for Caffe2 to work, and
6*da0073e9SAndroid Build Coastguard Workersometimes it is rather tricky. Specifically, we will need to have a
7*da0073e9SAndroid Build Coastguard Workerconsistent version between C++ and python simultaneously. This is a
8*da0073e9SAndroid Build Coastguard Workerconvenience script for one to quickly check if this is so on one's local
9*da0073e9SAndroid Build Coastguard Workermachine.
10*da0073e9SAndroid Build Coastguard Worker
11*da0073e9SAndroid Build Coastguard WorkerUsage:
12*da0073e9SAndroid Build Coastguard Worker    [set your environmental variables like PATH and PYTHONPATH]
13*da0073e9SAndroid Build Coastguard Worker    python scripts/diagnose_protobuf.py
14*da0073e9SAndroid Build Coastguard Worker"""
15*da0073e9SAndroid Build Coastguard Worker
16*da0073e9SAndroid Build Coastguard Workerimport os
17*da0073e9SAndroid Build Coastguard Workerimport re
18*da0073e9SAndroid Build Coastguard Workerfrom subprocess import PIPE, Popen
19*da0073e9SAndroid Build Coastguard Worker
20*da0073e9SAndroid Build Coastguard Worker
21*da0073e9SAndroid Build Coastguard Worker# Get python protobuf version.
22*da0073e9SAndroid Build Coastguard Workertry:
23*da0073e9SAndroid Build Coastguard Worker    import google.protobuf
24*da0073e9SAndroid Build Coastguard Worker
25*da0073e9SAndroid Build Coastguard Worker    python_version = google.protobuf.__version__
26*da0073e9SAndroid Build Coastguard Worker    python_protobuf_installed = True
27*da0073e9SAndroid Build Coastguard Workerexcept ImportError:
28*da0073e9SAndroid Build Coastguard Worker    print("DEBUG: cannot find python protobuf install.")
29*da0073e9SAndroid Build Coastguard Worker    python_protobuf_installed = False
30*da0073e9SAndroid Build Coastguard Worker
31*da0073e9SAndroid Build Coastguard Workerif os.name == "nt":
32*da0073e9SAndroid Build Coastguard Worker    protoc_name = "protoc.exe"
33*da0073e9SAndroid Build Coastguard Workerelse:
34*da0073e9SAndroid Build Coastguard Worker    protoc_name = "protoc"
35*da0073e9SAndroid Build Coastguard Worker
36*da0073e9SAndroid Build Coastguard Workertry:
37*da0073e9SAndroid Build Coastguard Worker    p = Popen([protoc_name, "--version"], stdout=PIPE, stderr=PIPE)
38*da0073e9SAndroid Build Coastguard Worker    out, err = p.communicate()
39*da0073e9SAndroid Build Coastguard Workerexcept:
40*da0073e9SAndroid Build Coastguard Worker    print("DEBUG: did not find protoc binary.")
41*da0073e9SAndroid Build Coastguard Worker    print("DEBUG: out: " + out)
42*da0073e9SAndroid Build Coastguard Worker    print("DEBUG: err: " + err)
43*da0073e9SAndroid Build Coastguard Worker    native_protobuf_installed = False
44*da0073e9SAndroid Build Coastguard Workerelse:
45*da0073e9SAndroid Build Coastguard Worker    if p.returncode:
46*da0073e9SAndroid Build Coastguard Worker        print("DEBUG: protoc returned a non-zero return code.")
47*da0073e9SAndroid Build Coastguard Worker        print("DEBUG: out: " + out)
48*da0073e9SAndroid Build Coastguard Worker        print("DEBUG: err: " + err)
49*da0073e9SAndroid Build Coastguard Worker        native_protobuf_installed = False
50*da0073e9SAndroid Build Coastguard Worker    else:
51*da0073e9SAndroid Build Coastguard Worker        tmp = re.search(r"\d\.\d\.\d", out)
52*da0073e9SAndroid Build Coastguard Worker        if tmp:
53*da0073e9SAndroid Build Coastguard Worker            native_version = tmp.group(0)
54*da0073e9SAndroid Build Coastguard Worker            native_protobuf_installed = True
55*da0073e9SAndroid Build Coastguard Worker        else:
56*da0073e9SAndroid Build Coastguard Worker            print("DEBUG: cannot parse protoc version string.")
57*da0073e9SAndroid Build Coastguard Worker            print("DEBUG: out: " + out)
58*da0073e9SAndroid Build Coastguard Worker            native_protobuf_installed = False
59*da0073e9SAndroid Build Coastguard Worker
60*da0073e9SAndroid Build Coastguard WorkerPYTHON_PROTOBUF_NOT_INSTALLED = """
61*da0073e9SAndroid Build Coastguard WorkerYou have not installed python protobuf. Protobuf is needed to run caffe2. You
62*da0073e9SAndroid Build Coastguard Workercan install protobuf via pip or conda (if you are using anaconda python).
63*da0073e9SAndroid Build Coastguard Worker"""
64*da0073e9SAndroid Build Coastguard Worker
65*da0073e9SAndroid Build Coastguard WorkerNATIVE_PROTOBUF_NOT_INSTALLED = """
66*da0073e9SAndroid Build Coastguard WorkerYou have not installed the protoc binary. Protoc is needed to compile Caffe2
67*da0073e9SAndroid Build Coastguard Workerprotobuf source files. Depending on the platform you are on, you can install
68*da0073e9SAndroid Build Coastguard Workerprotobuf via:
69*da0073e9SAndroid Build Coastguard Worker    (1) Mac: using homebrew and do brew install protobuf.
70*da0073e9SAndroid Build Coastguard Worker    (2) Linux: use apt and do apt-get install libprotobuf-dev
71*da0073e9SAndroid Build Coastguard Worker    (3) Windows: install from source, or from the releases here:
72*da0073e9SAndroid Build Coastguard Worker        https://github.com/google/protobuf/releases/
73*da0073e9SAndroid Build Coastguard Worker"""
74*da0073e9SAndroid Build Coastguard Worker
75*da0073e9SAndroid Build Coastguard WorkerVERSION_MISMATCH = f"""
76*da0073e9SAndroid Build Coastguard WorkerYour python protobuf is of version {python_version} but your native protoc version is of
77*da0073e9SAndroid Build Coastguard Workerversion {native_version}. This will cause the installation to produce incompatible
78*da0073e9SAndroid Build Coastguard Workerprotobuf files. This is bad in general - consider installing the same version.
79*da0073e9SAndroid Build Coastguard Worker"""
80*da0073e9SAndroid Build Coastguard Worker
81*da0073e9SAndroid Build Coastguard Worker# Now, give actual recommendations
82*da0073e9SAndroid Build Coastguard Workerif not python_protobuf_installed:
83*da0073e9SAndroid Build Coastguard Worker    print(PYTHON_PROTOBUF_NOT_INSTALLED)
84*da0073e9SAndroid Build Coastguard Worker
85*da0073e9SAndroid Build Coastguard Workerif not native_protobuf_installed:
86*da0073e9SAndroid Build Coastguard Worker    print(NATIVE_PROTOBUF_NOT_INSTALLED)
87*da0073e9SAndroid Build Coastguard Worker
88*da0073e9SAndroid Build Coastguard Workerif python_protobuf_installed and native_protobuf_installed:
89*da0073e9SAndroid Build Coastguard Worker    if python_version != native_version:
90*da0073e9SAndroid Build Coastguard Worker        print(VERSION_MISMATCH)
91*da0073e9SAndroid Build Coastguard Worker    else:
92*da0073e9SAndroid Build Coastguard Worker        print("All looks good.")
93