Skip to content

Commit 8424a35

Browse files
committed
added the ability to ban any substring tokens
1 parent 27a0907 commit 8424a35

File tree

3 files changed

+62
-2
lines changed

3 files changed

+62
-2
lines changed

expose.h

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33
const int stop_token_max = 10;
4+
const int ban_token_max = 10;
45
// match kobold's sampler list and order
56
enum samplers
67
{
@@ -35,6 +36,7 @@ struct load_model_inputs
3536
const int debugmode = 0;
3637
const int forceversion = 0;
3738
const int gpulayers = 0;
39+
const char * banned_tokens[ban_token_max];
3840
};
3941
struct generation_inputs
4042
{

gpttype_adapter.cpp

+49
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ static size_t mem_per_token = 0;
7676
static std::vector<float> logits;
7777
static std::vector<int> smartcontext;
7878
static std::vector<std::string> stop_sequence;
79+
static std::vector<std::string> banned_tokens;
80+
static std::vector<int> banned_token_ids;
7981
static std::vector<llama_token_data> top_picks;
8082
static int remaining_tokens = 0;
8183
static int stopper_unused_tokens = 0;
@@ -344,6 +346,17 @@ ModelLoadResult gpttype_load_model(const load_model_inputs inputs, FileFormat in
344346
= gpt2_ctx_v1.hparams.n_ctx = gpt2_ctx_v2.hparams.n_ctx = gpt2_ctx_v3.hparams.n_ctx
345347
= mpt_ctx_v3.hparams.n_ctx = params.n_ctx;
346348

349+
//handle custom token bans
350+
banned_tokens.clear();
351+
for(int x=0;x<ban_token_max;++x)
352+
{
353+
std::string word = inputs.banned_tokens[x];
354+
if(word!="")
355+
{
356+
banned_tokens.push_back(word);
357+
}
358+
}
359+
347360
//this is used for the mem_per_token eval, openblas needs more RAM
348361
bool use_scratch = ggml_cpu_has_gpublas();
349362

@@ -1064,6 +1077,25 @@ generation_outputs gpttype_generate(const generation_inputs inputs, generation_o
10641077
printf("Bad format!");
10651078
}
10661079

1080+
//prepare banned tokens
1081+
if(banned_token_ids.size()==0 && banned_tokens.size()>0)
1082+
{
1083+
printf("\n[First Run] Banning %d token sequences...",banned_tokens.size());
1084+
for(int v=0;v<n_vocab;++v)
1085+
{
1086+
std::string word = FileFormatTokenizeID(v,file_format);
1087+
for(int i=0;i<banned_tokens.size();++i)
1088+
{
1089+
if (word.find(banned_tokens[i]) != std::string::npos)
1090+
{
1091+
banned_token_ids.push_back(v);
1092+
break;
1093+
}
1094+
}
1095+
}
1096+
printf("\nBanned a total of %d tokens.\n",banned_token_ids.size());
1097+
}
1098+
10671099
if(debugmode!=-1)
10681100
{
10691101
printf("\n");
@@ -1221,6 +1253,7 @@ generation_outputs gpttype_generate(const generation_inputs inputs, generation_o
12211253

12221254
unsigned int eosID = 0;
12231255
float * logitsPtr;
1256+
int btsize = banned_token_ids.size();
12241257
if(file_format == FileFormat::GGML || file_format == FileFormat::GGHF || file_format == FileFormat::GGJT || file_format == FileFormat::GGJT_2 || file_format == FileFormat::GGJT_3)
12251258
{
12261259
if(file_format == FileFormat::GGJT_3)
@@ -1239,6 +1272,14 @@ generation_outputs gpttype_generate(const generation_inputs inputs, generation_o
12391272
// set the logit of the eos token (2) to zero to avoid sampling it
12401273
logitsPtr[eosID] = 0;
12411274
}
1275+
1276+
if(btsize>0)
1277+
{
1278+
for(int t=0;t<btsize;++t)
1279+
{
1280+
logitsPtr[banned_token_ids[t]]=0;
1281+
}
1282+
}
12421283
}
12431284
else
12441285
{
@@ -1293,6 +1334,14 @@ generation_outputs gpttype_generate(const generation_inputs inputs, generation_o
12931334
}
12941335
}
12951336

1337+
if(btsize>0)
1338+
{
1339+
int topid = std::min_element(logits.begin(), logits.end()) - logits.begin();
1340+
for (int t = 0; t < btsize; ++t)
1341+
{
1342+
logits[banned_token_ids[t]] = (logits[topid] < 0 ? logits[topid] : 0);
1343+
}
1344+
}
12961345
}
12971346

12981347
id = SampleLogits(logitsPtr, nctx, n_vocab, last_n_size, repeat_penalty,

koboldcpp.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
stop_token_max = 10
1515
sampler_order_max = 7
16+
ban_token_max = 10
1617

1718
class load_model_inputs(ctypes.Structure):
1819
_fields_ = [("threads", ctypes.c_int),
@@ -34,7 +35,8 @@ class load_model_inputs(ctypes.Structure):
3435
("blasbatchsize", ctypes.c_int),
3536
("debugmode", ctypes.c_int),
3637
("forceversion", ctypes.c_int),
37-
("gpulayers", ctypes.c_int)]
38+
("gpulayers", ctypes.c_int),
39+
("banned_tokens", ctypes.c_char_p * ban_token_max)]
3840

3941
class generation_inputs(ctypes.Structure):
4042
_fields_ = [("seed", ctypes.c_int),
@@ -195,6 +197,13 @@ def load_model(model_filename):
195197
inputs.cublas_info = 2
196198
inputs.executable_path = (getdirpath()+"/").encode("UTF-8")
197199
inputs.debugmode = args.debugmode
200+
banned_tokens = args.bantokens
201+
print(banned_tokens)
202+
for n in range(ban_token_max):
203+
if not banned_tokens or n >= len(banned_tokens):
204+
inputs.banned_tokens[n] = "".encode("UTF-8")
205+
else:
206+
inputs.banned_tokens[n] = banned_tokens[n].encode("UTF-8")
198207
ret = handle.load_model(inputs)
199208
return ret
200209

@@ -1297,7 +1306,7 @@ def main(args):
12971306
parser.add_argument("--stream", help="Uses streaming when generating tokens. Only for the Kobold Lite UI.", action='store_true')
12981307
parser.add_argument("--smartcontext", help="Reserving a portion of context to try processing less frequently.", action='store_true')
12991308
parser.add_argument("--unbantokens", help="Normally, KoboldAI prevents the EOS token from being generated. This flag unbans it.", action='store_true')
1300-
parser.add_argument("--bantokens", help="You can manually specify a list of token IDs that the AI cannot use.", metavar=('[elements]'), nargs='+')
1309+
parser.add_argument("--bantokens", help="You can manually specify a list of token SUBSTRINGS that the AI cannot use. This bans ALL instances of that substring.", metavar=('[token_substrings]'), nargs='+')
13011310
parser.add_argument("--usemirostat", help="Experimental! Replaces your samplers with mirostat. Takes 3 params = [type(0/1/2), tau(5.0), eta(0.1)].",metavar=('[type]', '[tau]', '[eta]'), type=float, nargs=3)
13021311
parser.add_argument("--forceversion", help="If the model file format detection fails (e.g. rogue modified model) you can set this to override the detected format (enter desired version, e.g. 401 for GPTNeoX-Type2).",metavar=('[version]'), type=int, default=0)
13031312
parser.add_argument("--nommap", help="If set, do not use mmap to load newer models", action='store_true')

0 commit comments

Comments
 (0)