Skip to content

Commit

Permalink
fix build;
Browse files Browse the repository at this point in the history
  • Loading branch information
chaoqing committed Dec 6, 2024
1 parent 2767ce5 commit 3e72c85
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 44 deletions.
56 changes: 35 additions & 21 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
.DEFAULT_GOAL := build
.DEFAULT_GOAL := example-tensorflow

#*****************************************#
#* VARIABLES *#
#*****************************************#
SHELL := /usr/bin/env bash

THIS_MAKEFILE := $(realpath $(lastword $(MAKEFILE_LIST)))
THIS_MAKEFILE_DIR := $(patsubst %/,%,$(dir $(THIS_MAKEFILE)))
Expand All @@ -15,10 +14,18 @@ ifeq ($(CPM_SOURCE_CACHE),)
CPM_SOURCE_CACHE := $(THIRD_PARTY_DIR)
endif

POETRY := POETRY_VIRTUALENVS_IN_PROJECT=true poetry
POETRY := poetry
PYTHON := $(POETRY) run python3
PYTHONPATH := $(REPO_DIR)/src
CMAKE := CPM_SOURCE_CACHE=$(CPM_SOURCE_CACHE) PATH=$(REPO_DIR)/.venv/bin:$(PATH) CC=clang CXX=clang++ cmake
CMAKE := cmake

ENVS := CC=clang CXX=clang++
ENVS += CPM_SOURCE_CACHE=$(CPM_SOURCE_CACHE)
ENVS += POETRY_VIRTUALENVS_IN_PROJECT=true
ENVS += PATH=$(REPO_DIR)/.venv/bin:$(PATH)
ENVS += LD_LIBRARY_PATH=$(THIRD_PARTY_DIR)/libtensorflow_cc/lib:$(LD_LIBRARY_PATH)

SHELL := /usr/bin/env $(ENVS) bash

#*****************************************#
#* UTILITIES *#
Expand All @@ -38,19 +45,31 @@ endef
example-pycxpress:
env -C $(REPO_DIR)/src/PyCXpress/example $(POETRY) run make run

example-tensorflow:
$(call message, Not implemented)
example-graph: ./sample/saved_model/saved_model.pb
./sample/saved_model/saved_model.pb: ./sample/main.py
env -C $(THIS_MAKEFILE_DIR)/sample $(PYTHON) main.py

example-tensorflow: build-sample example-graph
$(call message, Run ./build/sample/sample)
@env TF_CPP_MIN_LOG_LEVEL=2 $(THIS_MAKEFILE_DIR)/build/sample/sample --name whole_flow -- ./sample/saved_model/

example: example-pycxpress example-tensorflow

build: build-sample build-dist
rebuild: build-remove build
.NOTPARALLEL: rebuild

#*****************************************#
#* OPTIONS *#
#*****************************************#

CMAKE_OPTIONS :=

ifeq ($(CMAKE_PREFIX_PATH),)
CMAKE_OPTIONS += -DCMAKE_PREFIX_PATH="$(REPO_DIR)/third_party/libtorch;$(REPO_DIR)/third_party/libtensorflow_cc"
USE_LIBTENSORFLOW_CC := 1
ifeq ($(USE_LIBTENSORFLOW_CC),1)
ifeq ($(CMAKE_OPTIONS),)
CMAKE_OPTIONS += -DCMAKE_PREFIX_PATH="$(REPO_DIR)/third_party/libtorch;$(REPO_DIR)/third_party/libtensorflow_cc"
endif
endif

ifneq ($(TYPE),)
Expand Down Expand Up @@ -88,27 +107,21 @@ endif
#* ACTIONS *#
#*****************************************#

#* CMake
source-all: FORCE
$(call message, Clean)
rm -rf build
$(call message, Source)
$(CMAKE) -B build $(CMAKE_OPTIONS)

source: FORCE
source-sample: FORCE
$(call message, Source)
$(CMAKE) -S sample -B build/sample $(CMAKE_OPTIONS)

build: FORCE
build-sample: source-sample
$(call message, Build)
$(CMAKE) --build build/sample

graph: ./sample/main.py
env -C $(THIS_MAKEFILE_DIR)/sample $(PYTHON) main.py

run: build graph
$(call message, Run ./build/sample/sample)
@env TF_CPP_MIN_LOG_LEVEL=2 $(THIS_MAKEFILE_DIR)/build/sample/sample --name whole_flow -- ./sample/models/saved_model/

doc: FORCE
$(call message, Source)
$(CMAKE) -S doc -B build/doc $(CMAKE_OPTIONS)
Expand All @@ -119,12 +132,12 @@ doc: FORCE
#* Poetry
.PHONY: poetry-download
poetry-download:
curl -sSL https://install.python-poetry.org | $(PYTHON) -
curl -sSL https://install.python-poetry.org | python3 -
~/.local/share/pypoetry/venv/bin/pip install poetry-plugin-export

.PHONY: poetry-remove
poetry-remove:
curl -sSL https://install.python-poetry.org | $(PYTHON) - --uninstall
curl -sSL https://install.python-poetry.org | python3 - --uninstall

#* Installation
.PHONY: install
Expand All @@ -133,7 +146,7 @@ install:
$(POETRY) export --without-hashes > requirements.txt
$(POETRY) export -E tensorflow --without-hashes > requirements.tensorflow.txt
$(POETRY) install -n --extras tensorflow
-$(POETRY) run mypy --install-types --non-interactive ./
-$(POETRY) run mypy --install-types --non-interactive ./src

.PHONY: install-conda-deps install-conda-deps-manually
install-conda-deps:
Expand Down Expand Up @@ -198,7 +211,7 @@ check-codestyle:

.PHONY: mypy
mypy:
$(POETRY) run mypy --config-file pyproject.toml ./
$(POETRY) run mypy --config-file pyproject.toml ./src

.PHONY: check-safety
check-safety:
Expand Down Expand Up @@ -242,6 +255,7 @@ build-dist:
.PHONY: build-remove
build-remove:
rm -rf dist/
rm -rf build/

.PHONY: cleanup
cleanup: pycache-remove dsstore-remove mypycache-remove ipynbcheckpoints-remove pytestcache-remove
Expand Down
41 changes: 26 additions & 15 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ repository = "https://github.com/chaoqing/PyCXpress"
homepage = "https://github.com/chaoqing/PyCXpress"
packages = [
{ include = "PyCXpress", from = "src" },
{ include = "TensorflowCpy", from = "src" },
]


Expand Down Expand Up @@ -46,6 +47,9 @@ numpy = [
tensorflow = [
{version = "^2.10", python = "^3.9", source="jp61", optional = true}, # markers="platform_release == '5.15.148-tegra'",
]
protobuf = [
{version = "^3.9.2", python = "^3.9", optional = true},
]

[tool.poetry.extras]
tensorflow = [ "tensorflow" ]
Expand Down
2 changes: 1 addition & 1 deletion requirements.tensorflow.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ numpy==1.26.4 ; python_version >= "3.9" and python_version < "4.0"
opt-einsum==3.4.0 ; python_version >= "3.9" and python_version < "4.0"
optree==0.13.1 ; python_version >= "3.9" and python_version < "4.0"
packaging==24.2 ; python_version >= "3.9" and python_version < "4.0"
protobuf==4.25.5 ; python_version >= "3.9" and python_version < "4.0"
protobuf==3.20.3 ; python_version >= "3.9" and python_version < "4.0"
pybind11==2.13.6 ; python_version >= "3.8" and python_version < "4.0"
pygments==2.18.0 ; python_version >= "3.9" and python_version < "4.0"
requests==2.32.3 ; python_version >= "3.9" and python_version < "4.0"
Expand Down
2 changes: 2 additions & 0 deletions sample/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
frozen_graph/
saved_model/
16 changes: 13 additions & 3 deletions src/TensorflowCpy/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ endif()
if(ENABLE_TORCH)
find_package(Torch REQUIRED)
else()
find_package(TensorFlow REQUIRED)
if(USE_LIBTENSORFLOW_CC)
find_package(TensorFlow REQUIRED)
endif()
endif()

# ---- Add source files ----
Expand All @@ -43,12 +45,20 @@ target_include_directories(${PROJECT_NAME} PUBLIC $<BUILD_INTERFACE:${PROJECT_SO
if(ENABLE_TORCH)
target_include_directories(${PROJECT_NAME} PRIVATE ${TORCH_INCLUDE_DIRS})
else()
target_include_directories(${PROJECT_NAME} PRIVATE ${TensorFlow_INCLUDE_DIRS})
if(USE_LIBTENSORFLOW_CC)
target_include_directories(${PROJECT_NAME} PRIVATE ${TensorFlow_INCLUDE_DIRS})
else()
target_include_directories(
${PROJECT_NAME} PRIVATE $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include/tensorflow_cpy>
)
endif()
endif()

# link options
if(ENABLE_TORCH)
target_link_libraries(${PROJECT_NAME} PRIVATE ${TORCH_LIBRARIES})
else()
target_link_libraries(${PROJECT_NAME} PRIVATE ${TensorFlow_LIBRARIES})
if(USE_LIBTENSORFLOW_CC)
target_link_libraries(${PROJECT_NAME} PRIVATE ${TensorFlow_LIBRARIES})
endif()
endif()
Empty file added src/TensorflowCpy/__init__.py
Empty file.
8 changes: 4 additions & 4 deletions src/TensorflowCpy/source/framework.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include "tensorflow_cpy/framework.h"
#include <tensorflow_cpy/framework.h>

#include <filesystem>
#include <iostream>
#include <strstream>
#include <sstream>
#include <unordered_map>
namespace fs = std::filesystem;

Expand Down Expand Up @@ -166,11 +166,11 @@ int tensorflow_cpy::main_whole_flow(int argc, char** argv) {
auto gpuSubAllocator = std::make_unique<tf::DeviceMemAllocator>(
streamExecutor, deviceId, false, std::vector<tf::SubAllocator::Visitor>{},
std::vector<tf::SubAllocator::Visitor>{});
std::strstream name;
std::ostringstream name;
name << "GPU_" << deviceId.value() << "_bfc";
auto gpuAllocator = std::make_shared<tf::GPUBFCAllocator>(
std::move(gpuSubAllocator), ((size + alignment - 1) / alignment) * alignment,
std::string(name.str()), tf::GPUBFCAllocator::Options{});
name.str(), tf::GPUBFCAllocator::Options{});

return gpuAllocator;
};
Expand Down

0 comments on commit 3e72c85

Please sign in to comment.