Skip to content

Commit a6d04b8

Browse files
vegaluisjosetmoreau89
authored andcommitted
[RFC] [VTA] [TSIM] Enabling Cycle-Accurate Hardware Simulation for VTA #3009 (#3010)
* merge files * move verilator to the right place * change name to tsim * add default rule to be build and run * add README for tsim * Update README.md * add some structural feedback * change name of VTASim to VTADPISim * more renaming * update comment * add license * fix indentation * add switch for vta-tsim * add more licenses * update readme * address some of the new feedback * add some feedback from cpplint * add one more whitespace * pass pointer so linter is happy * pass pointer so linter is happy * README moved to vta documentation * create types for dpi functions, so they can be handle easily * fix pointer style * add feedback from docs * parametrize width data and pointers * fix comments * fix comment * add comment to class * add missing parameters * move README back to tsim example * add feedback * add more comments and remove un-necessary argument in finish * update comments * fix cpplint * fix doc
1 parent 981db15 commit a6d04b8

File tree

37 files changed

+2797
-0
lines changed

37 files changed

+2797
-0
lines changed

cmake/config.cmake

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,6 @@ set(USE_SORT ON)
131131

132132
# Build ANTLR parser for Relay text format
133133
set(USE_ANTLR OFF)
134+
135+
# Build TSIM for VTA
136+
set(USE_VTA_TSIM OFF)

cmake/modules/VTA.cmake

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@ elseif(PYTHON)
6060
find_library(__cma_lib NAMES cma PATH /usr/lib)
6161
target_link_libraries(vta ${__cma_lib})
6262
endif()
63+
64+
if(NOT USE_VTA_TSIM STREQUAL "OFF")
65+
include_directories("vta/include")
66+
file(GLOB RUNTIME_DPI_SRCS vta/src/dpi/module.cc)
67+
list(APPEND RUNTIME_SRCS ${RUNTIME_DPI_SRCS})
68+
endif()
69+
6370
else()
6471
message(STATUS "Cannot found python in env, VTA build is skipped..")
6572
endif()
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
cmake_minimum_required(VERSION 3.2)
19+
project(tsim C CXX)
20+
21+
set(TVM_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../../)
22+
set(VTA_DIR ${TVM_DIR}/vta)
23+
24+
include_directories("${TVM_DIR}/include")
25+
include_directories("${TVM_DIR}/3rdparty/dlpack/include")
26+
include_directories("${TVM_DIR}/3rdparty/dmlc-core/include")
27+
include_directories("${TVM_DIR}/vta/src/dpi")
28+
29+
set(CMAKE_C_FLAGS "-O2 -Wall -fPIC -fvisibility=hidden")
30+
set(CMAKE_CXX_FLAGS "-O2 -Wall -fPIC -fvisibility=hidden -std=c++11")
31+
32+
if (CMAKE_CXX_COMPILER_ID MATCHES "GNU" AND
33+
CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 7.0)
34+
set(CMAKE_CXX_FLAGS "-faligned-new ${CMAKE_CXX_FLAGS}")
35+
endif()
36+
37+
# Module rules
38+
include(cmake/modules/tsim.cmake)
39+
include(cmake/modules/driver.cmake)

vta/apps/tsim_example/Makefile

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
export PYTHONPATH:=$(PWD)/python:$(PYTHONPATH)
19+
20+
BUILD_DIR = $(shell python python/tsim/config.py --get-build-name)
21+
22+
TVM_DIR = $(abspath ../../../)
23+
24+
TSIM_TARGET = verilog
25+
TSIM_TOP_NAME = TestAccel
26+
TSIM_BUILD_NAME = build
27+
28+
# optional
29+
TSIM_TRACE_NAME = trace.vcd
30+
31+
default: cmake run
32+
33+
.PHONY: cmake
34+
35+
cmake: | $(BUILD_DIR)
36+
cd $(BUILD_DIR) && cmake .. && make
37+
38+
$(BUILD_DIR):
39+
mkdir -p $@
40+
41+
run:
42+
python3 tests/python/test_tsim.py | grep PASS
43+
44+
clean:
45+
-rm -rf $(BUILD_DIR)

vta/apps/tsim_example/README.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<!--- Licensed to the Apache Software Foundation (ASF) under one -->
2+
<!--- or more contributor license agreements. See the NOTICE file -->
3+
<!--- distributed with this work for additional information -->
4+
<!--- regarding copyright ownership. The ASF licenses this file -->
5+
<!--- to you under the Apache License, Version 2.0 (the -->
6+
<!--- "License"); you may not use this file except in compliance -->
7+
<!--- with the License. You may obtain a copy of the License at -->
8+
9+
<!--- http://www.apache.org/licenses/LICENSE-2.0 -->
10+
11+
<!--- Unless required by applicable law or agreed to in writing, -->
12+
<!--- software distributed under the License is distributed on an -->
13+
<!--- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -->
14+
<!--- KIND, either express or implied. See the License for the -->
15+
<!--- specific language governing permissions and limitations -->
16+
<!--- under the License. -->
17+
18+
VTA TSIM Installation
19+
======================
20+
21+
*TSIM* is a cycle-accurate hardware simulation environment that can be invoked and managed directly from TVM. It aims to enable cycle accurate simulation of deep learning accelerators including VTA.
22+
This simulation environment can be used in both OSX and Linux.
23+
There are two dependencies required to make *TSIM* works: [Verilator](https://www.veripool.org/wiki/verilator) and [sbt](https://www.scala-sbt.org/) for accelerators designed in [Chisel3](https://github.com/freechipsproject/chisel3).
24+
25+
## OSX Dependencies
26+
27+
Install `sbt` and `verilator` using [Homebrew](https://brew.sh/).
28+
29+
```bash
30+
brew install verilator sbt
31+
```
32+
33+
## Linux Dependencies
34+
35+
Add `sbt` to package manager (Ubuntu).
36+
37+
```bash
38+
echo "deb https://dl.bintray.com/sbt/debian /" | sudo tee -a /etc/apt/sources.list.d/sbt.list
39+
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 2EE0EA64E40A89B84B2DF73499E82A75642AC823
40+
sudo apt-get update
41+
```
42+
43+
Install `sbt` and `verilator`.
44+
45+
```bash
46+
sudo apt install verilator sbt
47+
```
48+
49+
## Setup in TVM
50+
51+
1. Install `verilator` and `sbt` as described above
52+
2. Enable VTA TSIM by turning on the switch `USE_VTA_TSIM` in config.cmake
53+
3. Build tvm
54+
55+
## How to run VTA TSIM examples
56+
57+
There are two sample VTA accelerators (add-by-one) designed in Chisel3 and Verilog to show how *TSIM* works.
58+
These examples are located at `<tvm-root>/vta/apps/tsim_example`.
59+
60+
* Instructions
61+
* Open `<tvm-root>/vta/apps/tsim_example/python/tsim/config.json`
62+
* Change `TARGET` from `verilog` to `chisel`, depending on what language backend you would like to test
63+
* Go to `tvm/vta/apps/tsim`
64+
* Run `make`
65+
66+
* Some pointers
67+
* Build cmake script for driver `<tvm-root>/vta/apps/tsim_example/cmake/modules/driver.cmake`
68+
* Build cmake script for tsim `<tvm-root>/vta/apps/tsim_example/cmake/modules/tsim.cmake`
69+
* Software driver that handles the VTA accelerator `<tvm-root>/vta/apps/tsim_example/src/driver.cc`
70+
* VTA add-by-one accelerator (Verilog) `<tvm-root>/vta/apps/tsim_example/hardware/verilog`
71+
* VTA add-by-one accelerator (Chisel) `<tvm-root>/vta/apps/tsim_example/hardware/chisel`
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
file(GLOB TSIM_SW_SRC src/driver.cc)
19+
add_library(driver SHARED ${TSIM_SW_SRC})
20+
target_include_directories(driver PRIVATE ${VTA_DIR}/include)
21+
22+
if(APPLE)
23+
set_target_properties(driver PROPERTIES LINK_FLAGS "-undefined dynamic_lookup")
24+
endif(APPLE)
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
if(MSVC)
19+
message(STATUS "TSIM build is skipped in Windows..")
20+
else()
21+
find_program(PYTHON NAMES python python3 python3.6)
22+
find_program(VERILATOR NAMES verilator)
23+
24+
if (VERILATOR AND PYTHON)
25+
26+
if (TSIM_TOP_NAME STREQUAL "")
27+
message(FATAL_ERROR "TSIM_TOP_NAME should be defined")
28+
endif()
29+
30+
if (TSIM_BUILD_NAME STREQUAL "")
31+
message(FATAL_ERROR "TSIM_BUILD_NAME should be defined")
32+
endif()
33+
34+
set(TSIM_CONFIG ${PYTHON} ${CMAKE_CURRENT_SOURCE_DIR}/python/tsim/config.py)
35+
36+
execute_process(COMMAND ${TSIM_CONFIG} --get-target OUTPUT_VARIABLE __TSIM_TARGET)
37+
execute_process(COMMAND ${TSIM_CONFIG} --get-top-name OUTPUT_VARIABLE __TSIM_TOP_NAME)
38+
execute_process(COMMAND ${TSIM_CONFIG} --get-build-name OUTPUT_VARIABLE __TSIM_BUILD_NAME)
39+
execute_process(COMMAND ${TSIM_CONFIG} --get-use-trace OUTPUT_VARIABLE __TSIM_USE_TRACE)
40+
execute_process(COMMAND ${TSIM_CONFIG} --get-trace-name OUTPUT_VARIABLE __TSIM_TRACE_NAME)
41+
42+
string(STRIP ${__TSIM_TARGET} TSIM_TARGET)
43+
string(STRIP ${__TSIM_TOP_NAME} TSIM_TOP_NAME)
44+
string(STRIP ${__TSIM_BUILD_NAME} TSIM_BUILD_NAME)
45+
string(STRIP ${__TSIM_USE_TRACE} TSIM_USE_TRACE)
46+
string(STRIP ${__TSIM_TRACE_NAME} TSIM_TRACE_NAME)
47+
48+
set(TSIM_BUILD_DIR ${CMAKE_CURRENT_SOURCE_DIR}/${TSIM_BUILD_NAME})
49+
50+
if (TSIM_TARGET STREQUAL "chisel")
51+
52+
find_program(SBT NAMES sbt)
53+
54+
if (SBT)
55+
56+
# Install Chisel VTA package for DPI modules
57+
set(VTA_CHISEL_DIR ${VTA_DIR}/hardware/chisel)
58+
59+
execute_process(WORKING_DIRECTORY ${VTA_CHISEL_DIR}
60+
COMMAND ${SBT} publishLocal RESULT_VARIABLE RETCODE)
61+
62+
if (NOT RETCODE STREQUAL "0")
63+
message(FATAL_ERROR "[TSIM] sbt failed to install VTA scala package")
64+
endif()
65+
66+
# Chisel - Scala to Verilog compilation
67+
set(TSIM_CHISEL_DIR ${CMAKE_CURRENT_SOURCE_DIR}/hardware/chisel)
68+
set(CHISEL_TARGET_DIR ${TSIM_BUILD_DIR}/chisel)
69+
set(CHISEL_OPT "test:runMain test.Elaborate --target-dir ${CHISEL_TARGET_DIR} --top-name ${TSIM_TOP_NAME}")
70+
71+
execute_process(WORKING_DIRECTORY ${TSIM_CHISEL_DIR} COMMAND ${SBT} ${CHISEL_OPT} RESULT_VARIABLE RETCODE)
72+
73+
if (NOT RETCODE STREQUAL "0")
74+
message(FATAL_ERROR "[TSIM] sbt failed to compile from Chisel to Verilog.")
75+
endif()
76+
77+
file(GLOB VERILATOR_RTL_SRC ${CHISEL_TARGET_DIR}/*.v)
78+
79+
else()
80+
message(FATAL_ERROR "[TSIM] sbt should be installed for Chisel")
81+
endif() # sbt
82+
83+
elseif (TSIM_TARGET STREQUAL "verilog")
84+
85+
set(VTA_VERILOG_DIR ${VTA_DIR}/hardware/chisel/src/main/resources/verilog)
86+
set(TSIM_VERILOG_DIR ${CMAKE_CURRENT_SOURCE_DIR}/hardware/verilog)
87+
file(GLOB VERILATOR_RTL_SRC ${VTA_VERILOG_DIR}/*.v ${TSIM_VERILOG_DIR}/*.v)
88+
89+
else()
90+
message(STATUS "[TSIM] target language can be only verilog or chisel...")
91+
endif() # TSIM_TARGET
92+
93+
if (TSIM_TARGET STREQUAL "chisel" OR TSIM_TARGET STREQUAL "verilog")
94+
95+
# Check if tracing can be enabled
96+
if (NOT TSIM_USE_TRACE STREQUAL "OFF")
97+
message(STATUS "[TSIM] Verilog enable tracing")
98+
else()
99+
message(STATUS "[TSIM] Verilator disable tracing")
100+
endif()
101+
102+
# Verilator - Verilog to C++ compilation
103+
set(VERILATOR_TARGET_DIR ${TSIM_BUILD_DIR}/verilator)
104+
set(VERILATOR_OPT +define+RANDOMIZE_GARBAGE_ASSIGN +define+RANDOMIZE_REG_INIT)
105+
list(APPEND VERILATOR_OPT +define+RANDOMIZE_MEM_INIT --x-assign unique)
106+
list(APPEND VERILATOR_OPT --output-split 20000 --output-split-cfuncs 20000)
107+
list(APPEND VERILATOR_OPT --top-module ${TSIM_TOP_NAME} -Mdir ${VERILATOR_TARGET_DIR})
108+
list(APPEND VERILATOR_OPT --cc ${VERILATOR_RTL_SRC})
109+
110+
if (NOT TSIM_USE_TRACE STREQUAL "OFF")
111+
list(APPEND VERILATOR_OPT --trace)
112+
endif()
113+
114+
execute_process(COMMAND ${VERILATOR} ${VERILATOR_OPT} RESULT_VARIABLE RETCODE)
115+
116+
if (NOT RETCODE STREQUAL "0")
117+
message(FATAL_ERROR "[TSIM] Verilator failed to compile Verilog to C++...")
118+
endif()
119+
120+
# Build shared library (.so)
121+
set(VTA_HW_DPI_DIR ${VTA_DIR}/hardware/dpi)
122+
set(VERILATOR_INC_DIR /usr/local/share/verilator/include)
123+
set(VERILATOR_LIB_SRC ${VERILATOR_INC_DIR}/verilated.cpp ${VERILATOR_INC_DIR}/verilated_dpi.cpp)
124+
125+
if (NOT TSIM_USE_TRACE STREQUAL "OFF")
126+
list(APPEND VERILATOR_LIB_SRC ${VERILATOR_INC_DIR}/verilated_vcd_c.cpp)
127+
endif()
128+
129+
file(GLOB VERILATOR_GEN_SRC ${VERILATOR_TARGET_DIR}/*.cpp)
130+
file(GLOB VERILATOR_SRC ${VTA_HW_DPI_DIR}/tsim_device.cc)
131+
add_library(tsim SHARED ${VERILATOR_LIB_SRC} ${VERILATOR_GEN_SRC} ${VERILATOR_SRC})
132+
133+
set(VERILATOR_DEF VL_TSIM_NAME=V${TSIM_TOP_NAME} VL_PRINTF=printf VM_COVERAGE=0 VM_SC=0)
134+
if (NOT TSIM_USE_TRACE STREQUAL "OFF")
135+
list(APPEND VERILATOR_DEF VM_TRACE=1 TSIM_TRACE_FILE=${TSIM_BUILD_DIR}/${TSIM_TRACE_NAME}.vcd)
136+
else()
137+
list(APPEND VERILATOR_DEF VM_TRACE=0)
138+
endif()
139+
target_compile_definitions(tsim PRIVATE ${VERILATOR_DEF})
140+
target_compile_options(tsim PRIVATE -Wno-sign-compare -include V${TSIM_TOP_NAME}.h)
141+
target_include_directories(tsim PRIVATE ${VERILATOR_TARGET_DIR} ${VERILATOR_INC_DIR} ${VERILATOR_INC_DIR}/vltstd ${VTA_DIR}/include)
142+
143+
if(APPLE)
144+
set_target_properties(tsim PROPERTIES LINK_FLAGS "-undefined dynamic_lookup")
145+
endif(APPLE)
146+
147+
endif() # TSIM_TARGET STREQUAL "chisel" OR TSIM_TARGET STREQUAL "verilog"
148+
149+
else()
150+
message(STATUS "[TSIM] could not find Python or Verilator, build is skipped...")
151+
endif() # VERILATOR
152+
endif() # MSVC
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
clean:
19+
-rm -rf target project/target project/project

0 commit comments

Comments
 (0)