|
| 1 | +# Copyright 2020 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +"""Tests for candidate_sampler_config_builder.""" |
| 15 | + |
| 16 | +from research.carls.candidate_sampling import candidate_sampler_config_builder as cs_config_builder |
| 17 | +from research.carls.candidate_sampling import candidate_sampler_config_pb2 as cs_config_pb2 |
| 18 | + |
| 19 | +import tensorflow as tf |
| 20 | + |
| 21 | + |
| 22 | +class CandidateSamplerConfigBuilderTest(tf.test.TestCase): |
| 23 | + |
| 24 | + def test_log_uniform_sampler(self): |
| 25 | + self.assertProtoEquals(""" |
| 26 | + unique: true |
| 27 | + """, cs_config_builder.log_uniform_sampler(True)) |
| 28 | + self.assertProtoEquals(""" |
| 29 | + unique: false |
| 30 | + """, cs_config_builder.log_uniform_sampler(False)) |
| 31 | + |
| 32 | + def test_brute_force_topk_sampler_success(self): |
| 33 | + self.assertProtoEquals(""" |
| 34 | + similarity_type: COSINE |
| 35 | + """, cs_config_builder.brute_force_topk_sampler('COSINE')) |
| 36 | + self.assertProtoEquals( |
| 37 | + """ |
| 38 | + similarity_type: COSINE |
| 39 | + """, cs_config_builder.brute_force_topk_sampler(cs_config_pb2.COSINE)) |
| 40 | + self.assertProtoEquals( |
| 41 | + """ |
| 42 | + similarity_type: DOT_PRODUCT |
| 43 | + """, cs_config_builder.brute_force_topk_sampler('DOT_PRODUCT')) |
| 44 | + self.assertProtoEquals( |
| 45 | + """ |
| 46 | + similarity_type: DOT_PRODUCT |
| 47 | + """, cs_config_builder.brute_force_topk_sampler(cs_config_pb2.DOT_PRODUCT)) |
| 48 | + |
| 49 | + def test_brute_force_topk_sampler_failed(self): |
| 50 | + with self.assertRaises(ValueError): |
| 51 | + cs_config_builder.brute_force_topk_sampler(cs_config_pb2.UNKNOWN) |
| 52 | + with self.assertRaises(ValueError): |
| 53 | + cs_config_builder.brute_force_topk_sampler('Unknown type string') |
| 54 | + with self.assertRaises(ValueError): |
| 55 | + cs_config_builder.brute_force_topk_sampler(cs_config_pb2.SampleContext()) |
| 56 | + with self.assertRaises(ValueError): |
| 57 | + cs_config_builder.brute_force_topk_sampler(999) |
| 58 | + |
| 59 | + def test_build_candidate_sampler_config_success(self): |
| 60 | + self.assertProtoEquals( |
| 61 | + """ |
| 62 | + extension { |
| 63 | + [type.googleapis.com/carls.candidate_sampling.BruteForceTopkSamplerConfig] { |
| 64 | + similarity_type: COSINE |
| 65 | + } |
| 66 | + } |
| 67 | + """, |
| 68 | + cs_config_builder.build_candidate_sampler_config( |
| 69 | + cs_config_builder.brute_force_topk_sampler('COSINE'))) |
| 70 | + |
| 71 | + self.assertProtoEquals( |
| 72 | + """ |
| 73 | + extension { |
| 74 | + [type.googleapis.com/carls.candidate_sampling.LogUniformSamplerConfig] { |
| 75 | + unique: true |
| 76 | + } |
| 77 | + } |
| 78 | + """, |
| 79 | + cs_config_builder.build_candidate_sampler_config( |
| 80 | + cs_config_builder.log_uniform_sampler(True))) |
| 81 | + |
| 82 | + def test_build_candidate_sampler_config_failed(self): |
| 83 | + with self.assertRaises(ValueError): |
| 84 | + cs_config_builder.build_candidate_sampler_config(100) |
| 85 | + with self.assertRaises(ValueError): |
| 86 | + cs_config_builder.build_candidate_sampler_config('invalid') |
| 87 | + |
| 88 | + |
| 89 | +if __name__ == '__main__': |
| 90 | + tf.test.main() |
0 commit comments