From 7c3a2be843fcff48bfb21c6ddbf4e2231093de8c Mon Sep 17 00:00:00 2001 From: Alham Fikri Aji Date: Tue, 10 Nov 2020 05:22:37 +0000 Subject: add regression tests for model quantization --- .../quantized-model/quantized-no-opt.expected | 10 +++++ .../quantized-model/quantized-with-bias.expected | 10 +++++ .../features/quantized-model/quantized.expected | 10 +++++ .../features/quantized-model/test_quantmodel.sh | 37 ++++++++++++++++++ .../quantized-model/test_quantmodel_with_bias.sh | 44 ++++++++++++++++++++++ .../test_quantmodel_with_optimization.sh | 37 ++++++++++++++++++ 6 files changed, 148 insertions(+) create mode 100644 tests/training/features/quantized-model/quantized-no-opt.expected create mode 100644 tests/training/features/quantized-model/quantized-with-bias.expected create mode 100644 tests/training/features/quantized-model/quantized.expected create mode 100644 tests/training/features/quantized-model/test_quantmodel.sh create mode 100644 tests/training/features/quantized-model/test_quantmodel_with_bias.sh create mode 100644 tests/training/features/quantized-model/test_quantmodel_with_optimization.sh (limited to 'tests') diff --git a/tests/training/features/quantized-model/quantized-no-opt.expected b/tests/training/features/quantized-model/quantized-no-opt.expected new file mode 100644 index 0000000..c4ac602 --- /dev/null +++ b/tests/training/features/quantized-model/quantized-no-opt.expected @@ -0,0 +1,10 @@ +225.10929871 +243.58345032 +229.45071411 +224.28813171 +212.65242004 +204.06596375 +197.81690979 +190.08908081 +193.72296143 +195.20832825 diff --git a/tests/training/features/quantized-model/quantized-with-bias.expected b/tests/training/features/quantized-model/quantized-with-bias.expected new file mode 100644 index 0000000..4f56c41 --- /dev/null +++ b/tests/training/features/quantized-model/quantized-with-bias.expected @@ -0,0 +1,10 @@ +225.10006714 +243.58285522 +229.47399902 +224.31018066 +212.68742371 +204.09915161 +197.85253906 +190.12380981 +193.74653625 +195.23658752 diff --git a/tests/training/features/quantized-model/quantized.expected b/tests/training/features/quantized-model/quantized.expected new file mode 100644 index 0000000..5f42a2c --- /dev/null +++ b/tests/training/features/quantized-model/quantized.expected @@ -0,0 +1,10 @@ +225.25198364 +244.07009888 +230.06900024 +224.89492798 +213.12133789 +204.42272949 +198.22579956 +190.56004333 +194.42613220 +196.06826782 diff --git a/tests/training/features/quantized-model/test_quantmodel.sh b/tests/training/features/quantized-model/test_quantmodel.sh new file mode 100644 index 0000000..75d3a1a --- /dev/null +++ b/tests/training/features/quantized-model/test_quantmodel.sh @@ -0,0 +1,37 @@ +#!/bin/bash -x + +##################################################################### +# SUMMARY: Train a quantized marian model +# AUTHOR: afaji +##################################################################### + +# Exit on error +set -e + +PREFIX=quantized + +# Remove old artifacts and create working directory +rm -rf $PREFIX $PREFIX.{log,out,diff} +mkdir -p $PREFIX + + +# Train an 8-bits model +$MRT_MARIAN/marian \ + --no-shuffle --seed 1111 --dim-emb 32 --dim-rnn 64 --mini-batch 32 --maxi-batch 1 --maxi-batch-sort none --learn-rate 0.1 --optimizer sgd \ + -m $PREFIX/model.npz -t $MRT_DATA/europarl.de-en/corpus.bpe.{en,de} -v train/vocab.en.yml train/vocab.de.yml \ + --cost-type cross-entropy --sync-sgd --after-batches 100 --disp-freq 10 --quantize-bits 8 \ + --log $PREFIX.log + +# Check if files exist +test -e $PREFIX/model.npz +test -e $PREFIX.log + +# Compare the current output with the expected output +cat $PREFIX.log | $MRT_TOOLS/extract-costs.sh > $PREFIX.out +$MRT_TOOLS/diff-nums.py $PREFIX.out $PREFIX.expected -o $PREFIX.diff + +# make sure that the resulting model has no more than 256 different values (i.e. quantized) +$MRT_TOOLS/check-model-unique-vals.py $PREFIX/model.npz -b 8 + +# Exit with success code +exit 0 diff --git a/tests/training/features/quantized-model/test_quantmodel_with_bias.sh b/tests/training/features/quantized-model/test_quantmodel_with_bias.sh new file mode 100644 index 0000000..1878ecb --- /dev/null +++ b/tests/training/features/quantized-model/test_quantmodel_with_bias.sh @@ -0,0 +1,44 @@ +#!/bin/bash -x + +##################################################################### +# SUMMARY: Train a quantized marian model +# AUTHOR: afaji +##################################################################### + +# Exit on error +set -e + +PREFIX=quantized-with-bias + +# Remove old artifacts and create working directory +rm -rf $PREFIX $PREFIX.{log,out,diff} +mkdir -p $PREFIX + + +# training with quantized bias is tricky, so we start by training a normal model first before finetuning it to the quantized space. +$MRT_MARIAN/marian \ + --no-shuffle --seed 1111 --dim-emb 32 --dim-rnn 64 --mini-batch 32 --maxi-batch 1 --maxi-batch-sort none --learn-rate 0.1 --optimizer sgd \ + -m $PREFIX/model.npz -t $MRT_DATA/europarl.de-en/corpus.bpe.{en,de} -v train/vocab.en.yml train/vocab.de.yml \ + --cost-type cross-entropy --sync-sgd --after-batches 20 --disp-freq 10 \ + --log $PREFIX.log + +# Train an 8-bits model +$MRT_MARIAN/marian \ + --no-shuffle --seed 1111 --dim-emb 32 --dim-rnn 64 --mini-batch 32 --maxi-batch 1 --maxi-batch-sort none --learn-rate 0.1 --optimizer sgd \ + -m $PREFIX/model.npz -t $MRT_DATA/europarl.de-en/corpus.bpe.{en,de} -v train/vocab.en.yml train/vocab.de.yml \ + --cost-type cross-entropy --sync-sgd --after-batches 100 --disp-freq 10 --quantize-bits 8 --quantize-biases \ + --log $PREFIX.log + +# Check if files exist +test -e $PREFIX/model.npz +test -e $PREFIX.log + +# Compare the current output with the expected output +cat $PREFIX.log | $MRT_TOOLS/extract-costs.sh > $PREFIX.out +$MRT_TOOLS/diff-nums.py $PREFIX.out $PREFIX.expected -o $PREFIX.diff + +# make sure that the resulting model has no more than 256 different values (i.e. quantized) +$MRT_TOOLS/check-model-unique-vals.py $PREFIX/model.npz -b 8 --with_bias + +# Exit with success code +exit 0 diff --git a/tests/training/features/quantized-model/test_quantmodel_with_optimization.sh b/tests/training/features/quantized-model/test_quantmodel_with_optimization.sh new file mode 100644 index 0000000..368d920 --- /dev/null +++ b/tests/training/features/quantized-model/test_quantmodel_with_optimization.sh @@ -0,0 +1,37 @@ +#!/bin/bash -x + +##################################################################### +# SUMMARY: Train a quantized marian model with a scale optimization +# AUTHOR: afaji +##################################################################### + +# Exit on error +set -e + +PREFIX=quantized-opt + +# Remove old artifacts and create working directory +rm -rf $PREFIX $PREFIX.{log,out,diff} +mkdir -p $PREFIX + + +# Train an 8-bits model +$MRT_MARIAN/marian \ + --no-shuffle --seed 1111 --dim-emb 32 --dim-rnn 64 --mini-batch 32 --maxi-batch 1 --maxi-batch-sort none --learn-rate 0.1 --optimizer sgd \ + -m $PREFIX/model.npz -t $MRT_DATA/europarl.de-en/corpus.bpe.{en,de} -v train/vocab.en.yml train/vocab.de.yml \ + --cost-type cross-entropy --sync-sgd --after-batches 100 --disp-freq 10 --quantize-bits 8 --quantize-optimization-steps 3 \ + --log $PREFIX.log + +# Check if files exist +test -e $PREFIX/model.npz +test -e $PREFIX.log + +# Compare the current output with the expected output +cat $PREFIX.log | $MRT_TOOLS/extract-costs.sh > $PREFIX.out +$MRT_TOOLS/diff-nums.py $PREFIX.out $PREFIX.expected -o $PREFIX.diff + +# make sure that the resulting model has no more than 256 different values (i.e. quantized) +$MRT_TOOLS/check-model-unique-vals.py $PREFIX/model.npz -b 8 + +# Exit with success code +exit 0 -- cgit v1.2.3