optimizing, profiling, and tuning tensorflow+ gpus · introductions: me §chris fregly, research...
TRANSCRIPT
![Page 1: OPTIMIZING, PROFILING, AND TUNING TENSORFLOW+ GPUS · INTRODUCTIONS: ME §Chris Fregly, Research Engineer @ §Formerly Netflixand Databricks §Advanced Spark and TensorFlowMeetup](https://reader036.vdocuments.us/reader036/viewer/2022071019/5fd3629ac88123605963a2b1/html5/thumbnails/1.jpg)
OPTIMIZING, PROFILING, AND TUNINGTENSORFLOW + GPUS
NVIDIA GPU TECH CONFMUNICH, GERMANYOCTOBER 11, 2017
CHRIS FREGLY, FOUNDER @ PIPELINE.AI
![Page 2: OPTIMIZING, PROFILING, AND TUNING TENSORFLOW+ GPUS · INTRODUCTIONS: ME §Chris Fregly, Research Engineer @ §Formerly Netflixand Databricks §Advanced Spark and TensorFlowMeetup](https://reader036.vdocuments.us/reader036/viewer/2022071019/5fd3629ac88123605963a2b1/html5/thumbnails/2.jpg)
INTRODUCTIONS: ME§ Chris Fregly, Research Engineer @
§ Formerly Netflix and Databricks
§ Advanced Spark and TensorFlow MeetupPlease Join Our 40,000+ Members Globally!
* San Francisco* Chicago* Washington DC* London
Contact [email protected]
@cfregly
![Page 3: OPTIMIZING, PROFILING, AND TUNING TENSORFLOW+ GPUS · INTRODUCTIONS: ME §Chris Fregly, Research Engineer @ §Formerly Netflixand Databricks §Advanced Spark and TensorFlowMeetup](https://reader036.vdocuments.us/reader036/viewer/2022071019/5fd3629ac88123605963a2b1/html5/thumbnails/3.jpg)
INTRODUCTIONS: YOU
§ Software Engineer or Data Scientist interested in optimizing and deploying TensorFlow models to production
§ Assume you have a working knowledge of TensorFlow
![Page 4: OPTIMIZING, PROFILING, AND TUNING TENSORFLOW+ GPUS · INTRODUCTIONS: ME §Chris Fregly, Research Engineer @ §Formerly Netflixand Databricks §Advanced Spark and TensorFlowMeetup](https://reader036.vdocuments.us/reader036/viewer/2022071019/5fd3629ac88123605963a2b1/html5/thumbnails/4.jpg)
CONTENT BREAKDOWN
§ 50% Training Optimizations (GPUs, Pipeline, XLA+JIT)§ 50% Prediction Optimizations (XLA+AOT, TF Serving)
§ Why Heavy Focus on Predicting?§ Training: boring batch O(num_data_scientists)§ Inference: exciting real-time O(num_users_of_app)
![Page 5: OPTIMIZING, PROFILING, AND TUNING TENSORFLOW+ GPUS · INTRODUCTIONS: ME §Chris Fregly, Research Engineer @ §Formerly Netflixand Databricks §Advanced Spark and TensorFlowMeetup](https://reader036.vdocuments.us/reader036/viewer/2022071019/5fd3629ac88123605963a2b1/html5/thumbnails/5.jpg)
100% OPEN SOURCE CODE§ https://github.com/PipelineAI/pipeline/
§ Please 🌟 this GitHub Repo!
§ All slides, code, notebooks, and Docker images here:https://github.com/PipelineAI/pipeline/tree/master/gpu
![Page 6: OPTIMIZING, PROFILING, AND TUNING TENSORFLOW+ GPUS · INTRODUCTIONS: ME §Chris Fregly, Research Engineer @ §Formerly Netflixand Databricks §Advanced Spark and TensorFlowMeetup](https://reader036.vdocuments.us/reader036/viewer/2022071019/5fd3629ac88123605963a2b1/html5/thumbnails/6.jpg)
AGENDA§ Optimize TensorFlow Training
§ GPUs§ Ingestion Pipeline§ XLA JIT Compiler
§ Optimize TensorFlow Inference§ XLA AOT Compiler§ Graph Transform Tool (GTT)§ TensorFlow Serving
§ Compare Optimizations in Production
![Page 7: OPTIMIZING, PROFILING, AND TUNING TENSORFLOW+ GPUS · INTRODUCTIONS: ME §Chris Fregly, Research Engineer @ §Formerly Netflixand Databricks §Advanced Spark and TensorFlowMeetup](https://reader036.vdocuments.us/reader036/viewer/2022071019/5fd3629ac88123605963a2b1/html5/thumbnails/7.jpg)
SETTING UP TENSORFLOW WITH GPUS
§ Very Painful!
§ Especially inside Docker§ Use nvidia-docker
§ Especially on Kubernetes!§ Use Kubernetes 1.7+
§ http://pipeline.ai for GitHub + DockerHub Links
![Page 8: OPTIMIZING, PROFILING, AND TUNING TENSORFLOW+ GPUS · INTRODUCTIONS: ME §Chris Fregly, Research Engineer @ §Formerly Netflixand Databricks §Advanced Spark and TensorFlowMeetup](https://reader036.vdocuments.us/reader036/viewer/2022071019/5fd3629ac88123605963a2b1/html5/thumbnails/8.jpg)
GPU HALF-PRECISION SUPPORT§ FP16, INT8 are “Half Precision”§ Supported by Pascal P100 (2016) and Volta V100 (2017)§ Flexible FP32 GPU Cores Can Fit 2 FP16’s for 2x Throughput!§ Half-Precision is OK for Approximate Deep Learning Use Cases
![Page 9: OPTIMIZING, PROFILING, AND TUNING TENSORFLOW+ GPUS · INTRODUCTIONS: ME §Chris Fregly, Research Engineer @ §Formerly Netflixand Databricks §Advanced Spark and TensorFlowMeetup](https://reader036.vdocuments.us/reader036/viewer/2022071019/5fd3629ac88123605963a2b1/html5/thumbnails/9.jpg)
VOLTA V100 RECENTLY ANNOUNCED§ 84 Streaming Multiprocessors (SM’s)§ 5,376 GPU Cores§ 672 Tensor Cores (ie. Google TPU)
§ Mixed FP16/FP32 Precision § More Shared Memory§ New L0 Instruction Cache§ Faster L1 Data Cache§ V100 vs. P100 Performance
§ 12x TFLOPS @ Peak Training§ 6x Inference Throughput
![Page 10: OPTIMIZING, PROFILING, AND TUNING TENSORFLOW+ GPUS · INTRODUCTIONS: ME §Chris Fregly, Research Engineer @ §Formerly Netflixand Databricks §Advanced Spark and TensorFlowMeetup](https://reader036.vdocuments.us/reader036/viewer/2022071019/5fd3629ac88123605963a2b1/html5/thumbnails/10.jpg)
V100 AND CUDA 9§ Independent Thread Scheduling - Finally!!
§ Similar to CPU fine-grained thread synchronization semantics§ Allows GPU to yield execution of any thread
§ Still Optimized for SIMT (Same Instruction Multiple Thread)§ SIMT units automatically scheduled together
§ Explicit Synchronization
P100 V100
![Page 11: OPTIMIZING, PROFILING, AND TUNING TENSORFLOW+ GPUS · INTRODUCTIONS: ME §Chris Fregly, Research Engineer @ §Formerly Netflixand Databricks §Advanced Spark and TensorFlowMeetup](https://reader036.vdocuments.us/reader036/viewer/2022071019/5fd3629ac88123605963a2b1/html5/thumbnails/11.jpg)
CUDA STREAMS
§ Asynchronous I/O Transfer§ Overlap Compute and I/O§ Keeps GPUs Saturated§ Fundamental to Queue Framework in TensorFlow
![Page 12: OPTIMIZING, PROFILING, AND TUNING TENSORFLOW+ GPUS · INTRODUCTIONS: ME §Chris Fregly, Research Engineer @ §Formerly Netflixand Databricks §Advanced Spark and TensorFlowMeetup](https://reader036.vdocuments.us/reader036/viewer/2022071019/5fd3629ac88123605963a2b1/html5/thumbnails/12.jpg)
AGENDA§ Optimize TensorFlow Training
§ GPUs§ Ingestion Pipeline§ XLA JIT Compiler
§ Optimize TensorFlow Inference§ XLA AOT Compiler§ Graph Transform Tool (GTT)§ TensorFlow Serving
§ Compare Optimizations in Production
![Page 13: OPTIMIZING, PROFILING, AND TUNING TENSORFLOW+ GPUS · INTRODUCTIONS: ME §Chris Fregly, Research Engineer @ §Formerly Netflixand Databricks §Advanced Spark and TensorFlowMeetup](https://reader036.vdocuments.us/reader036/viewer/2022071019/5fd3629ac88123605963a2b1/html5/thumbnails/13.jpg)
EXISTING DATA PIPELINES§ Data Processing
§ HDFS/Hadoop§ Spark
§ Containers§ Docker§ Google Container
§ Container Orchestrators§ Kubernetes§ Mesos
<dependency> <groupId>org.tensorflow</groupId> <artifactId>tensorflow-hadoop</artifactId>
</dependency>
https://github.com/tensorflow/ecosystem
![Page 14: OPTIMIZING, PROFILING, AND TUNING TENSORFLOW+ GPUS · INTRODUCTIONS: ME §Chris Fregly, Research Engineer @ §Formerly Netflixand Databricks §Advanced Spark and TensorFlowMeetup](https://reader036.vdocuments.us/reader036/viewer/2022071019/5fd3629ac88123605963a2b1/html5/thumbnails/14.jpg)
DON’T USE FEED_DICT
§ Not Optimized for Production Pipelines§ feed_dict Requires Python <-> C++ Serialization§ Single-threaded, Synchronous, SLOW!§ Can’t Retrieve Until Current Batch is Complete§ CPUs/GPUs Not Fully Utilized!§ Use Queue or Dataset API
![Page 15: OPTIMIZING, PROFILING, AND TUNING TENSORFLOW+ GPUS · INTRODUCTIONS: ME §Chris Fregly, Research Engineer @ §Formerly Netflixand Databricks §Advanced Spark and TensorFlowMeetup](https://reader036.vdocuments.us/reader036/viewer/2022071019/5fd3629ac88123605963a2b1/html5/thumbnails/15.jpg)
QUEUES
§ More than just a traditional Queue§ Perform I/O, pre-processing, cropping, shuffling§ Pulls from HDFS, S3, Google Storage, Kafka, ...§ Combine many small files into large TFRecord files§ Use CPUs to free GPUs for compute§ Uses CUDA Streams§ Helps saturate CPUs and GPUs
![Page 16: OPTIMIZING, PROFILING, AND TUNING TENSORFLOW+ GPUS · INTRODUCTIONS: ME §Chris Fregly, Research Engineer @ §Formerly Netflixand Databricks §Advanced Spark and TensorFlowMeetup](https://reader036.vdocuments.us/reader036/viewer/2022071019/5fd3629ac88123605963a2b1/html5/thumbnails/16.jpg)
QUEUE CAPACITY PLANNING§ batch_size
§ # examples / batch (ie. 64 jpg)§ Limited by GPU RAM
§ num_processing_threads§ CPU threads pull and pre-process batches of data§ Limited by CPU Cores
§ queue_capacity§ Limited by CPU RAM (ie. 5 * batch_size)
![Page 17: OPTIMIZING, PROFILING, AND TUNING TENSORFLOW+ GPUS · INTRODUCTIONS: ME §Chris Fregly, Research Engineer @ §Formerly Netflixand Databricks §Advanced Spark and TensorFlowMeetup](https://reader036.vdocuments.us/reader036/viewer/2022071019/5fd3629ac88123605963a2b1/html5/thumbnails/17.jpg)
DETECT UNDERUTILIZED CPUS, GPUS
§ Instrument training code to generate “timelines”
§ Analyze with Google Web Tracing Framework (WTF)
§ Monitor CPU with `top`, GPU with `nvidia-smi`
http://google.github.io/tracing-framework/
from tensorflow.python.client import timeline
trace = timeline.Timeline(step_stats=run_metadata.step_stats)
with open('timeline.json', 'w') as trace_file:trace_file.write(trace.generate_chrome_trace_format(show_memory=True))
![Page 18: OPTIMIZING, PROFILING, AND TUNING TENSORFLOW+ GPUS · INTRODUCTIONS: ME §Chris Fregly, Research Engineer @ §Formerly Netflixand Databricks §Advanced Spark and TensorFlowMeetup](https://reader036.vdocuments.us/reader036/viewer/2022071019/5fd3629ac88123605963a2b1/html5/thumbnails/18.jpg)
SINGLE NODE, MULTI-GPU TRAINING§ cpu:0
§ By default, all CPUs§ Requires extra config to target a CPU
§ gpu:0..n§ Each GPU has a unique id§ TF usually prefers a single GPU
§ xla_cpu:0, xla_gpu:0..n§ “JIT Compiler Device”§ Hints TensorFlow to attempt JIT Compile
with tf.device(“/cpu:0”):
with tf.device(“/gpu:0”):
with tf.device(“/gpu:1”):
GPU 0 GPU 1
![Page 19: OPTIMIZING, PROFILING, AND TUNING TENSORFLOW+ GPUS · INTRODUCTIONS: ME §Chris Fregly, Research Engineer @ §Formerly Netflixand Databricks §Advanced Spark and TensorFlowMeetup](https://reader036.vdocuments.us/reader036/viewer/2022071019/5fd3629ac88123605963a2b1/html5/thumbnails/19.jpg)
MULTI-NODE DISTRIBUTED TRAINING§ TensorFlow Automatically Inserts Send and Receive Ops into Graph§ Parameter Server Synchronously Aggregates Updates to Variables§ Nodes with Multiple GPUs will Pre-Aggregate Before Sending to PS
Worker0 Worker0
Worker1
Worker0 Worker1 Worker2
gpu0 gpu1
gpu2 gpu3
gpu0 gpu1
gpu2 gpu3
gpu0 gpu1
gpu2 gpu3
gpu0
gpu1
gpu0
gpu0
![Page 20: OPTIMIZING, PROFILING, AND TUNING TENSORFLOW+ GPUS · INTRODUCTIONS: ME §Chris Fregly, Research Engineer @ §Formerly Netflixand Databricks §Advanced Spark and TensorFlowMeetup](https://reader036.vdocuments.us/reader036/viewer/2022071019/5fd3629ac88123605963a2b1/html5/thumbnails/20.jpg)
SYNCHRONOUS VS. ASYNCHRONOUS§ Synchronous
§ Nodes compute gradients§ Nodes update Parameter Server (PS)§ Nodes sync on PS for latest gradients
§ Asynchronous§ Some nodes delay in computing gradients§ Nodes don’t update PS§ Nodes get stale gradients from PS§ May not converge due to stale reads!
![Page 21: OPTIMIZING, PROFILING, AND TUNING TENSORFLOW+ GPUS · INTRODUCTIONS: ME §Chris Fregly, Research Engineer @ §Formerly Netflixand Databricks §Advanced Spark and TensorFlowMeetup](https://reader036.vdocuments.us/reader036/viewer/2022071019/5fd3629ac88123605963a2b1/html5/thumbnails/21.jpg)
SEPARATE TRAINING + VALIDATION
§ Separate Training and Validation Clusters § Validate using Saved Checkpoints from Parameter Servers§ Avoids Resource Contention
TrainingCluster
ValidationCluster
Parameter ServerCluster
![Page 22: OPTIMIZING, PROFILING, AND TUNING TENSORFLOW+ GPUS · INTRODUCTIONS: ME §Chris Fregly, Research Engineer @ §Formerly Netflixand Databricks §Advanced Spark and TensorFlowMeetup](https://reader036.vdocuments.us/reader036/viewer/2022071019/5fd3629ac88123605963a2b1/html5/thumbnails/22.jpg)
ALWAYS USE BATCH NORMALIZATION
§ Each Mini-Batch May Have Wildly Different Distributions§ Normalize per batch (and layer)§ Speeds up Training!!§ Weights are Learned Quicker§ Final Model is More Accurate§ Final mean and variance will be folded into Graph later
-- Always Use Batch Normalization! --
z = tf.matmul(a_prev, W)a = tf.nn.relu(z)
a_mean, a_var = tf.nn.moments(a, [0])
scale = tf.Variable(tf.ones([depth/channels]))beta = tf.Variable(tf.zeros ([depth/channels]))
bn = tf.nn.batch_normalizaton(a, a_mean, a_var, beta, scale, 0.001)
![Page 23: OPTIMIZING, PROFILING, AND TUNING TENSORFLOW+ GPUS · INTRODUCTIONS: ME §Chris Fregly, Research Engineer @ §Formerly Netflixand Databricks §Advanced Spark and TensorFlowMeetup](https://reader036.vdocuments.us/reader036/viewer/2022071019/5fd3629ac88123605963a2b1/html5/thumbnails/23.jpg)
OPTIMIZE GRAPH EXECUTION ORDER
§ https://github.com/yaroslavvb/stuff
Linearize to minimize graphmemory usage
![Page 24: OPTIMIZING, PROFILING, AND TUNING TENSORFLOW+ GPUS · INTRODUCTIONS: ME §Chris Fregly, Research Engineer @ §Formerly Netflixand Databricks §Advanced Spark and TensorFlowMeetup](https://reader036.vdocuments.us/reader036/viewer/2022071019/5fd3629ac88123605963a2b1/html5/thumbnails/24.jpg)
AGENDA§ Optimize TensorFlow Training
§ GPUs§ Ingestion Pipeline§ XLA JIT Compiler
§ Optimize TensorFlow Inference§ XLA AOT Compiler§ Graph Transform Tool (GTT)§ TensorFlow Serving
§ Compare Optimizations in Production
![Page 25: OPTIMIZING, PROFILING, AND TUNING TENSORFLOW+ GPUS · INTRODUCTIONS: ME §Chris Fregly, Research Engineer @ §Formerly Netflixand Databricks §Advanced Spark and TensorFlowMeetup](https://reader036.vdocuments.us/reader036/viewer/2022071019/5fd3629ac88123605963a2b1/html5/thumbnails/25.jpg)
XLA FRAMEWORK§ Accelerated Linear Algebra (XLA)§ Goals:
§ Reduce reliance on custom operators§ Improve execution speed§ Improve memory usage§ Reduce mobile footprint§ Improve portability
§ Helps TensorFlow Stay Both Flexible and Performant
![Page 26: OPTIMIZING, PROFILING, AND TUNING TENSORFLOW+ GPUS · INTRODUCTIONS: ME §Chris Fregly, Research Engineer @ §Formerly Netflixand Databricks §Advanced Spark and TensorFlowMeetup](https://reader036.vdocuments.us/reader036/viewer/2022071019/5fd3629ac88123605963a2b1/html5/thumbnails/26.jpg)
XLA HIGH LEVEL OPTIMIZER (HLO)
§ Compiler Intermediate Representation (IR)§ Independent of Source and Target Language§ Define Graphs using HLO Operations§ XLA Step 1 Emits Target-Independent HLO § XLA Step 2 Emits Target-Dependent LLVM§ LLVM Emits Native Code Specific to Target § Supports x86-64, ARM64 (CPU), and NVPTX (GPU)
![Page 27: OPTIMIZING, PROFILING, AND TUNING TENSORFLOW+ GPUS · INTRODUCTIONS: ME §Chris Fregly, Research Engineer @ §Formerly Netflixand Databricks §Advanced Spark and TensorFlowMeetup](https://reader036.vdocuments.us/reader036/viewer/2022071019/5fd3629ac88123605963a2b1/html5/thumbnails/27.jpg)
JIT COMPILER§ Just-In-Time Compiler§ Built on XLA Framework§ Goals:
§ Reduce memory movement – especially useful on GPUs§ Reduce overhead of multiple function calls
§ Similar to Spark Operator Fusing in Spark 2.0§ Unroll Loops, Fuse Operators, Fold Constants, …§ Scope to session, device, or `with jit_scope():`
![Page 28: OPTIMIZING, PROFILING, AND TUNING TENSORFLOW+ GPUS · INTRODUCTIONS: ME §Chris Fregly, Research Engineer @ §Formerly Netflixand Databricks §Advanced Spark and TensorFlowMeetup](https://reader036.vdocuments.us/reader036/viewer/2022071019/5fd3629ac88123605963a2b1/html5/thumbnails/28.jpg)
VISUALIZING JIT COMPILER IN ACTION
Before After
Google Web Tracing Framework:http://google.github.io/tracing-framework/
from tensorflow.python.client import timelinetrace = timeline.Timeline(step_stats=run_metadata.step_stats)with open('timeline.json', 'w') as trace_file:trace_file.write(
trace.generate_chrome_trace_format(show_memory=True))
![Page 29: OPTIMIZING, PROFILING, AND TUNING TENSORFLOW+ GPUS · INTRODUCTIONS: ME §Chris Fregly, Research Engineer @ §Formerly Netflixand Databricks §Advanced Spark and TensorFlowMeetup](https://reader036.vdocuments.us/reader036/viewer/2022071019/5fd3629ac88123605963a2b1/html5/thumbnails/29.jpg)
VISUALIZING FUSING OPERATORS
pip install graphviz
dot -Tpng \/tmp/hlo_graph_1.w5LcGs.dot \-o hlo_graph_1.png
GraphViz:http://www.graphviz.org
hlo_*.dot files generated by XLA
![Page 30: OPTIMIZING, PROFILING, AND TUNING TENSORFLOW+ GPUS · INTRODUCTIONS: ME §Chris Fregly, Research Engineer @ §Formerly Netflixand Databricks §Advanced Spark and TensorFlowMeetup](https://reader036.vdocuments.us/reader036/viewer/2022071019/5fd3629ac88123605963a2b1/html5/thumbnails/30.jpg)
AGENDA§ Optimize TensorFlow Training
§ GPUs§ Ingestion Pipeline§ XLA JIT Compiler
§ Optimize TensorFlow Inference§ XLA AOT Compiler§ Graph Transform Tool (GTT)§ TensorFlow Serving
§ Compare Optimizations in Production
![Page 31: OPTIMIZING, PROFILING, AND TUNING TENSORFLOW+ GPUS · INTRODUCTIONS: ME §Chris Fregly, Research Engineer @ §Formerly Netflixand Databricks §Advanced Spark and TensorFlowMeetup](https://reader036.vdocuments.us/reader036/viewer/2022071019/5fd3629ac88123605963a2b1/html5/thumbnails/31.jpg)
AOT COMPILER§ Standalone, Ahead-Of-Time (AOT) Compiler§ Built on XLA framework§ tfcompile§ Creates executable with minimal TensorFlow Runtime needed
§ Includes only dependencies needed by subgraph computation§ Creates functions with feeds (inputs) and fetches (outputs)
§ Packaged as cc_libary header and object files to link into your app§ Commonly used for mobile device inference graph
§ Currently, only CPU x86-64 and ARM are supported - no GPU
![Page 32: OPTIMIZING, PROFILING, AND TUNING TENSORFLOW+ GPUS · INTRODUCTIONS: ME §Chris Fregly, Research Engineer @ §Formerly Netflixand Databricks §Advanced Spark and TensorFlowMeetup](https://reader036.vdocuments.us/reader036/viewer/2022071019/5fd3629ac88123605963a2b1/html5/thumbnails/32.jpg)
AGENDA§ Optimize TensorFlow Training
§ GPUs§ Ingestion Pipeline§ XLA JIT Compiler
§ Optimize TensorFlow Inference§ XLA AOT Compiler§ Graph Transform Tool (GTT)§ TensorFlow Serving
§ Compare Optimizations in Production
![Page 33: OPTIMIZING, PROFILING, AND TUNING TENSORFLOW+ GPUS · INTRODUCTIONS: ME §Chris Fregly, Research Engineer @ §Formerly Netflixand Databricks §Advanced Spark and TensorFlowMeetup](https://reader036.vdocuments.us/reader036/viewer/2022071019/5fd3629ac88123605963a2b1/html5/thumbnails/33.jpg)
GRAPH TRANSFORM TOOL (GTT)
§ Optimize Trained Models for Inference§ Remove training-only Ops (checkpoint, drop out, logs)§ Remove unreachable nodes between given feed -> fetch§ Fuse adjacent operators to improve memory bandwidth§ Fold final batch norm mean and variance into variables§ Round weights/variables improves compression (ie. 70%)§ Quantize (FP32 -> INT8) to speed up math operations
![Page 34: OPTIMIZING, PROFILING, AND TUNING TENSORFLOW+ GPUS · INTRODUCTIONS: ME §Chris Fregly, Research Engineer @ §Formerly Netflixand Databricks §Advanced Spark and TensorFlowMeetup](https://reader036.vdocuments.us/reader036/viewer/2022071019/5fd3629ac88123605963a2b1/html5/thumbnails/34.jpg)
BEFORE OPTIMIZATIONS
![Page 35: OPTIMIZING, PROFILING, AND TUNING TENSORFLOW+ GPUS · INTRODUCTIONS: ME §Chris Fregly, Research Engineer @ §Formerly Netflixand Databricks §Advanced Spark and TensorFlowMeetup](https://reader036.vdocuments.us/reader036/viewer/2022071019/5fd3629ac88123605963a2b1/html5/thumbnails/35.jpg)
GRAPH TRANSFORM TOOL
transform_graph \--in_graph=tensorflow_inception_graph.pb \ ß Original Graph--out_graph=optimized_inception_graph.pb \ ß Transformed Graph--inputs='Mul' \ ß Feed (Input)--outputs='softmax' \ ß Fetch (Output) --transforms=' ß List of Transforms strip_unused_nodesremove_nodes(op=Identity, op=CheckNumerics) fold_constants(ignore_errors=true) fold_batch_normsfold_old_batch_normsquantize_weightsquantize_nodes'
![Page 36: OPTIMIZING, PROFILING, AND TUNING TENSORFLOW+ GPUS · INTRODUCTIONS: ME §Chris Fregly, Research Engineer @ §Formerly Netflixand Databricks §Advanced Spark and TensorFlowMeetup](https://reader036.vdocuments.us/reader036/viewer/2022071019/5fd3629ac88123605963a2b1/html5/thumbnails/36.jpg)
AFTER STRIPPING UNUSED NODES
§ Optimizations§ strip_unused_nodes
§ Results§ Graph much simpler§ File size much smaller
![Page 37: OPTIMIZING, PROFILING, AND TUNING TENSORFLOW+ GPUS · INTRODUCTIONS: ME §Chris Fregly, Research Engineer @ §Formerly Netflixand Databricks §Advanced Spark and TensorFlowMeetup](https://reader036.vdocuments.us/reader036/viewer/2022071019/5fd3629ac88123605963a2b1/html5/thumbnails/37.jpg)
AFTER REMOVING UNUSED NODES
§ Optimizations§ strip_unused_nodes§ remove_nodes
§ Results§ Pesky nodes removed§ File size a bit smaller
![Page 38: OPTIMIZING, PROFILING, AND TUNING TENSORFLOW+ GPUS · INTRODUCTIONS: ME §Chris Fregly, Research Engineer @ §Formerly Netflixand Databricks §Advanced Spark and TensorFlowMeetup](https://reader036.vdocuments.us/reader036/viewer/2022071019/5fd3629ac88123605963a2b1/html5/thumbnails/38.jpg)
AFTER FOLDING CONSTANTS
§ Optimizations§ strip_unused_nodes§ remove_nodes§ fold_constants
§ Results§ Placeholders (feeds) -> Variables*
(*Why Variables and not Constants?)
![Page 39: OPTIMIZING, PROFILING, AND TUNING TENSORFLOW+ GPUS · INTRODUCTIONS: ME §Chris Fregly, Research Engineer @ §Formerly Netflixand Databricks §Advanced Spark and TensorFlowMeetup](https://reader036.vdocuments.us/reader036/viewer/2022071019/5fd3629ac88123605963a2b1/html5/thumbnails/39.jpg)
AFTER FOLDING BATCH NORMS
§ Optimizations§ strip_unused_nodes§ remove_nodes§ fold_constants§ fold_batch_norms
§ Results§ Graph remains the same§ File size approximately the same
![Page 40: OPTIMIZING, PROFILING, AND TUNING TENSORFLOW+ GPUS · INTRODUCTIONS: ME §Chris Fregly, Research Engineer @ §Formerly Netflixand Databricks §Advanced Spark and TensorFlowMeetup](https://reader036.vdocuments.us/reader036/viewer/2022071019/5fd3629ac88123605963a2b1/html5/thumbnails/40.jpg)
WEIGHT QUANTIZATION
§ FP16 and INT8 Are Smaller and Computationally Simpler§ Weights/Variables are Constants§ Easy to Linearly Quantize
![Page 41: OPTIMIZING, PROFILING, AND TUNING TENSORFLOW+ GPUS · INTRODUCTIONS: ME §Chris Fregly, Research Engineer @ §Formerly Netflixand Databricks §Advanced Spark and TensorFlowMeetup](https://reader036.vdocuments.us/reader036/viewer/2022071019/5fd3629ac88123605963a2b1/html5/thumbnails/41.jpg)
AFTER QUANTIZING WEIGHTS
§ Optimizations§ strip_unused_nodes§ remove_nodes§ fold_constants§ fold_batch_norms§ quantize_weights
§ Results§ Graph is same, file size is smaller, compute is faster
![Page 42: OPTIMIZING, PROFILING, AND TUNING TENSORFLOW+ GPUS · INTRODUCTIONS: ME §Chris Fregly, Research Engineer @ §Formerly Netflixand Databricks §Advanced Spark and TensorFlowMeetup](https://reader036.vdocuments.us/reader036/viewer/2022071019/5fd3629ac88123605963a2b1/html5/thumbnails/42.jpg)
BUT WAIT, THERE’S MORE!
![Page 43: OPTIMIZING, PROFILING, AND TUNING TENSORFLOW+ GPUS · INTRODUCTIONS: ME §Chris Fregly, Research Engineer @ §Formerly Netflixand Databricks §Advanced Spark and TensorFlowMeetup](https://reader036.vdocuments.us/reader036/viewer/2022071019/5fd3629ac88123605963a2b1/html5/thumbnails/43.jpg)
ACTIVATION QUANTIZATION§ Activations Not Known Ahead of Time
§ Depends on input, not easy to quantize§ Requires Additional Calibration Step
§ Use a “representative” dataset§ Per Neural Network Layer…
§ Collect histogram of activation values§ Generate many quantized distributions with different saturation thresholds§ Choose threshold to minimize…
KL_divergence(ref_distribution, quant_distribution)
§ Not Much Time or Data is Required (Minutes on Commodity Hardware)
![Page 44: OPTIMIZING, PROFILING, AND TUNING TENSORFLOW+ GPUS · INTRODUCTIONS: ME §Chris Fregly, Research Engineer @ §Formerly Netflixand Databricks §Advanced Spark and TensorFlowMeetup](https://reader036.vdocuments.us/reader036/viewer/2022071019/5fd3629ac88123605963a2b1/html5/thumbnails/44.jpg)
AFTER ACTIVATION QUANTIZATION
§ Optimizations§ strip_unused_nodes§ remove_nodes§ fold_constants§ fold_batch_norms§ quantize_weights§ quantize_nodes (activations)
§ Results§ Larger graph, needs calibration!
Requires additional freeze_requantization_ranges
![Page 45: OPTIMIZING, PROFILING, AND TUNING TENSORFLOW+ GPUS · INTRODUCTIONS: ME §Chris Fregly, Research Engineer @ §Formerly Netflixand Databricks §Advanced Spark and TensorFlowMeetup](https://reader036.vdocuments.us/reader036/viewer/2022071019/5fd3629ac88123605963a2b1/html5/thumbnails/45.jpg)
FREEZING MODEL FOR DEPLOYMENT§ Optimizations
§ strip_unused_nodes§ remove_nodes§ fold_constants§ fold_batch_norms§ quantize_weights§ quantize_nodes§ freeze_graph
§ Results§ Variables -> Constants
Finally!We’re Ready to Deploy!!
![Page 46: OPTIMIZING, PROFILING, AND TUNING TENSORFLOW+ GPUS · INTRODUCTIONS: ME §Chris Fregly, Research Engineer @ §Formerly Netflixand Databricks §Advanced Spark and TensorFlowMeetup](https://reader036.vdocuments.us/reader036/viewer/2022071019/5fd3629ac88123605963a2b1/html5/thumbnails/46.jpg)
AGENDA§ Optimize TensorFlow Training
§ GPUs§ Ingestion Pipeline§ XLA JIT Compiler
§ Optimize TensorFlow Inference§ XLA AOT Compiler§ Graph Transform Tool (GTT)§ TensorFlow Serving
§ Compare Optimizations in Production
![Page 47: OPTIMIZING, PROFILING, AND TUNING TENSORFLOW+ GPUS · INTRODUCTIONS: ME §Chris Fregly, Research Engineer @ §Formerly Netflixand Databricks §Advanced Spark and TensorFlowMeetup](https://reader036.vdocuments.us/reader036/viewer/2022071019/5fd3629ac88123605963a2b1/html5/thumbnails/47.jpg)
TENSORFLOW SERVING OVERVIEW§ Inference
§ Only Forward Propagation through Network§ Predict, Classify, Regress, …
§ Bundle§ GraphDef, Variables, Metadata, …
§ Assets§ ie. Map of ClassificationID -> String§ {9283: “penguin”, 9284: “bridge”}
§ Version§ Every Model Has a Version Number (Integer)
§ Version Policy§ ie. Serve Only Latest (Highest), Serve Both Latest and Previous, …
![Page 48: OPTIMIZING, PROFILING, AND TUNING TENSORFLOW+ GPUS · INTRODUCTIONS: ME §Chris Fregly, Research Engineer @ §Formerly Netflixand Databricks §Advanced Spark and TensorFlowMeetup](https://reader036.vdocuments.us/reader036/viewer/2022071019/5fd3629ac88123605963a2b1/html5/thumbnails/48.jpg)
MULTI-HEADED INFERENCE
§ Multiple “heads” (aka “responses”) from 1 model prediction§ Response includes both class and scores § Inputs sent only once§ Feed scores into ensemble models§ Use model for feature engineering§ Optimizes bandwidth, CPU, latency, memory, coolness
![Page 49: OPTIMIZING, PROFILING, AND TUNING TENSORFLOW+ GPUS · INTRODUCTIONS: ME §Chris Fregly, Research Engineer @ §Formerly Netflixand Databricks §Advanced Spark and TensorFlowMeetup](https://reader036.vdocuments.us/reader036/viewer/2022071019/5fd3629ac88123605963a2b1/html5/thumbnails/49.jpg)
REQUEST BATCHING§ max_batch_size
§ Enables throughput/latency tradeoff§ Bounded by RAM
§ batch_timeout_micros§ Defines batch time window, latency upper-bound§ Bounded by RAM
§ num_batch_threads§ Defines parallelism§ Bounded by CPU cores
§ max_enqueued_batches§ Defines queue upper bound, throttling§ Bounded by RAM
Reaching either thresholdwill trigger a batch
![Page 50: OPTIMIZING, PROFILING, AND TUNING TENSORFLOW+ GPUS · INTRODUCTIONS: ME §Chris Fregly, Research Engineer @ §Formerly Netflixand Databricks §Advanced Spark and TensorFlowMeetup](https://reader036.vdocuments.us/reader036/viewer/2022071019/5fd3629ac88123605963a2b1/html5/thumbnails/50.jpg)
TENSORRT RUNTIME(NVIDIA)
§ Post-Training Model Optimizations§ Alternative to Graph Transform Tool
§ GPU-Optimized Prediction Runtime§ Alternative to TensorFlow Serving
![Page 51: OPTIMIZING, PROFILING, AND TUNING TENSORFLOW+ GPUS · INTRODUCTIONS: ME §Chris Fregly, Research Engineer @ §Formerly Netflixand Databricks §Advanced Spark and TensorFlowMeetup](https://reader036.vdocuments.us/reader036/viewer/2022071019/5fd3629ac88123605963a2b1/html5/thumbnails/51.jpg)
AGENDA§ Optimize TensorFlow Training
§ GPUs§ Ingestion Pipeline§ XLA JIT Compiler
§ Optimize TensorFlow Inference§ XLA AOT Compiler§ Graph Transform Tool (GTT)§ TensorFlow Serving
§ Compare Optimizations in Production
![Page 52: OPTIMIZING, PROFILING, AND TUNING TENSORFLOW+ GPUS · INTRODUCTIONS: ME §Chris Fregly, Research Engineer @ §Formerly Netflixand Databricks §Advanced Spark and TensorFlowMeetup](https://reader036.vdocuments.us/reader036/viewer/2022071019/5fd3629ac88123605963a2b1/html5/thumbnails/52.jpg)
COMPARE OPTIMIZATIONS IN PROD
![Page 53: OPTIMIZING, PROFILING, AND TUNING TENSORFLOW+ GPUS · INTRODUCTIONS: ME §Chris Fregly, Research Engineer @ §Formerly Netflixand Databricks §Advanced Spark and TensorFlowMeetup](https://reader036.vdocuments.us/reader036/viewer/2022071019/5fd3629ac88123605963a2b1/html5/thumbnails/53.jpg)
FAST AND EASY MODEL EXPERIMENTS§ Create Experiments with Drag n’ Drop
§ Deploy Safely into Production
§ Control Traffic Routing
![Page 54: OPTIMIZING, PROFILING, AND TUNING TENSORFLOW+ GPUS · INTRODUCTIONS: ME §Chris Fregly, Research Engineer @ §Formerly Netflixand Databricks §Advanced Spark and TensorFlowMeetup](https://reader036.vdocuments.us/reader036/viewer/2022071019/5fd3629ac88123605963a2b1/html5/thumbnails/54.jpg)
360º MODEL COMPARISON§ Compare Models Offline and Online
§ Offline Training and Validation Accuracy
§ Real-Time Prediction Precision
§ Real-Time Prediction Response Time
![Page 55: OPTIMIZING, PROFILING, AND TUNING TENSORFLOW+ GPUS · INTRODUCTIONS: ME §Chris Fregly, Research Engineer @ §Formerly Netflixand Databricks §Advanced Spark and TensorFlowMeetup](https://reader036.vdocuments.us/reader036/viewer/2022071019/5fd3629ac88123605963a2b1/html5/thumbnails/55.jpg)
AUTOMATIC TRAFFIC SHIFTING§ Dynamically Route Traffic to MAX(Revenue)
![Page 56: OPTIMIZING, PROFILING, AND TUNING TENSORFLOW+ GPUS · INTRODUCTIONS: ME §Chris Fregly, Research Engineer @ §Formerly Netflixand Databricks §Advanced Spark and TensorFlowMeetup](https://reader036.vdocuments.us/reader036/viewer/2022071019/5fd3629ac88123605963a2b1/html5/thumbnails/56.jpg)
AUTOMATIC TRAFFIC SHIFTING§ Auto-Shift Traffic to MIN(Cost)
§ Real-Time Cost Per Prediction
§ Across Clouds and On-Premise
![Page 57: OPTIMIZING, PROFILING, AND TUNING TENSORFLOW+ GPUS · INTRODUCTIONS: ME §Chris Fregly, Research Engineer @ §Formerly Netflixand Databricks §Advanced Spark and TensorFlowMeetup](https://reader036.vdocuments.us/reader036/viewer/2022071019/5fd3629ac88123605963a2b1/html5/thumbnails/57.jpg)
PREDICTION PROFILING + TUNING§ Pinpoint Performance Bottlenecks
§ Fine-Grained Prediction Metrics
§ 3 Logic Steps in a Prediction1. transform_request()2. predict()3. transform_response()
![Page 58: OPTIMIZING, PROFILING, AND TUNING TENSORFLOW+ GPUS · INTRODUCTIONS: ME §Chris Fregly, Research Engineer @ §Formerly Netflixand Databricks §Advanced Spark and TensorFlowMeetup](https://reader036.vdocuments.us/reader036/viewer/2022071019/5fd3629ac88123605963a2b1/html5/thumbnails/58.jpg)
LIVE PREDICTION STREAMS§ Visually Compare Real-Time Predictions
![Page 59: OPTIMIZING, PROFILING, AND TUNING TENSORFLOW+ GPUS · INTRODUCTIONS: ME §Chris Fregly, Research Engineer @ §Formerly Netflixand Databricks §Advanced Spark and TensorFlowMeetup](https://reader036.vdocuments.us/reader036/viewer/2022071019/5fd3629ac88123605963a2b1/html5/thumbnails/59.jpg)
CONTINUOUS MODEL TRAINING§ Identify and Fix Borderline Predictions (50-50% Confidence)
§ Fix Along Class Boundaries
§ Enables Crowd Sourcing
§ Game-ify Tedious Process
§ Retrain on New Labeled Data
![Page 60: OPTIMIZING, PROFILING, AND TUNING TENSORFLOW+ GPUS · INTRODUCTIONS: ME §Chris Fregly, Research Engineer @ §Formerly Netflixand Databricks §Advanced Spark and TensorFlowMeetup](https://reader036.vdocuments.us/reader036/viewer/2022071019/5fd3629ac88123605963a2b1/html5/thumbnails/60.jpg)
AUTOMATIC MODEL OPTIMIZATIONS§ Generate Optimized Model Versions
§ Weight + Activation Quantization
§ CPU + GPU Runtime Optimizations
§ TensorFlow, TensorRT (Nvidia), etc
![Page 61: OPTIMIZING, PROFILING, AND TUNING TENSORFLOW+ GPUS · INTRODUCTIONS: ME §Chris Fregly, Research Engineer @ §Formerly Netflixand Databricks §Advanced Spark and TensorFlowMeetup](https://reader036.vdocuments.us/reader036/viewer/2022071019/5fd3629ac88123605963a2b1/html5/thumbnails/61.jpg)
OPTIMIZE BOTH MODEL + RUNTIME§ Build Model + Runtime into Immutable Docker Image
§ Same Runtime: Local, Dev, Test, Prod
§ No Dependency Surprises in Production
§ Tune Model + Runtime Together as One
§ Hyper-Parameter Tuning includes Runtime Config and Metrics
![Page 62: OPTIMIZING, PROFILING, AND TUNING TENSORFLOW+ GPUS · INTRODUCTIONS: ME §Chris Fregly, Research Engineer @ §Formerly Netflixand Databricks §Advanced Spark and TensorFlowMeetup](https://reader036.vdocuments.us/reader036/viewer/2022071019/5fd3629ac88123605963a2b1/html5/thumbnails/62.jpg)
AGENDA§ Optimize TensorFlow Training
§ GPUs§ Ingestion Pipeline§ XLA JIT Compiler
§ Optimize TensorFlow Inference§ XLA AOT Compiler§ Graph Transform Tool (GTT)§ TensorFlow Serving
§ Compare Optimizations in Production
![Page 63: OPTIMIZING, PROFILING, AND TUNING TENSORFLOW+ GPUS · INTRODUCTIONS: ME §Chris Fregly, Research Engineer @ §Formerly Netflixand Databricks §Advanced Spark and TensorFlowMeetup](https://reader036.vdocuments.us/reader036/viewer/2022071019/5fd3629ac88123605963a2b1/html5/thumbnails/63.jpg)
DANKE SHOEN! HABEN FRAGEN?§ https://github.com/PipelineAI/pipeline/
§ Please 🌟 this GitHub Repo!
§ All slides, code, notebooks, and Docker images here:https://github.com/PipelineAI/pipeline/tree/master/gpu
Contact [email protected]
@cfregly