This commit is contained in:
parent
940134f4c9
commit
5b6b54f6c0
|
|
@ -9,6 +9,7 @@ from tqdm import tqdm
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import pickle
|
import pickle
|
||||||
# from utils import get_llama_activations_bau, tokenized_tqa, tokenized_tqa_gen, tokenized_tqa_gen_end_q
|
# from utils import get_llama_activations_bau, tokenized_tqa, tokenized_tqa_gen, tokenized_tqa_gen_end_q
|
||||||
|
from utils import mahalanobis_distance
|
||||||
import llama_iti
|
import llama_iti
|
||||||
import pickle
|
import pickle
|
||||||
import argparse
|
import argparse
|
||||||
|
|
@ -390,37 +391,40 @@ def main():
|
||||||
centered_h=torch.from_numpy(centered_h).cuda()
|
centered_h=torch.from_numpy(centered_h).cuda()
|
||||||
centered_t=torch.from_numpy(centered_t).cuda()
|
centered_t=torch.from_numpy(centered_t).cuda()
|
||||||
_, sin_value, V_p = torch.linalg.svd(centered, full_matrices=False)
|
_, sin_value, V_p = torch.linalg.svd(centered, full_matrices=False)
|
||||||
sin_value_squared = torch.diag(sin_value[:svd]) ** 2
|
sin_value_squared = torch.diag(sin_value[:k]) ** 2
|
||||||
V_p = V_p[:svd, :]
|
V_p = V_p[:k, :]
|
||||||
C=(1 / centered.shape[0])* V_p.T @ sin_value_squared @ V_p
|
C=(1 / centered.shape[0])* V_p.T @ sin_value_squared @ V_p
|
||||||
|
|
||||||
|
|
||||||
_, sin_value_h, V_p_h = torch.linalg.svd(centered_h, full_matrices=False)
|
_, sin_value_h, V_p_h = torch.linalg.svd(centered_h, full_matrices=False)
|
||||||
sin_value_h_squared = torch.diag(sin_value_h[:svd]) ** 2
|
sin_value_h_squared = torch.diag(sin_value_h[:k]) ** 2
|
||||||
V_p_h = V_p_h[:svd, :]
|
V_p_h = V_p_h[:k, :]
|
||||||
C_h=(1 / centered_h.shape[0])* V_p_h.T @ sin_value_h_squared @ V_p_h
|
C_h=(1 / centered_h.shape[0])* V_p_h.T @ sin_value_h_squared @ V_p_h
|
||||||
|
|
||||||
# print(centered_t.shape)
|
# print(centered_t.shape)
|
||||||
_, sin_value_t, V_p_t = torch.linalg.svd(centered_t, full_matrices=False)
|
_, sin_value_t, V_p_t = torch.linalg.svd(centered_t, full_matrices=False)
|
||||||
sin_value_t_squared = torch.diag(sin_value_t[:svd]) ** 2
|
sin_value_t_squared = torch.diag(sin_value_t[:k]) ** 2
|
||||||
V_p_t = V_p_t[:svd, :]
|
V_p_t = V_p_t[:k, :]
|
||||||
C_t=(1 / centered_t.shape[0])* V_p_t.T @ sin_value_t_squared @ V_p_t
|
C_t=(1 / centered_t.shape[0])* V_p_t.T @ sin_value_t_squared @ V_p_t
|
||||||
inv_C= torch.linalg.inv(C) + torch.eye(C.shape[0], dtype=int).cuda() * epsilon
|
|
||||||
inv_C_t= torch.linalg.inv(C_t) + torch.eye(C_t.shape[0], dtype=int).cuda() * epsilon
|
|
||||||
inv_C_h= torch.linalg.inv(C_h) + torch.eye(C_h.shape[0], dtype=int).cuda() * epsilon
|
|
||||||
scores= torch.sqrt(centered @ inv_C @ centered.T) - torch.sqrt(torch.from_numpy(embed_generated[:, layer, :]-mean_t).cuda() @ inv_C_t @ torch.from_numpy(embed_generated[:, layer, :]-mean_t).cuda().T)
|
inv_C_t= torch.linalg.pinv(C_t) + torch.eye(C_t.shape[0], dtype=int).cuda() * epsilon
|
||||||
+ torch.sqrt(torch.from_numpy(embed_generated[:, layer, :]-mean_h).cuda() @ inv_C_h @ torch.from_numpy(embed_generated[:, layer, :]-mean_h).cuda().T)
|
inv_C_h= torch.linalg.pinv(C_h) + torch.eye(C_h.shape[0], dtype=int).cuda() * epsilon
|
||||||
print(torch.isnan(scores).any())
|
scores= torch.sqrt(torch.clamp(centered @ inv_C_t @ centered.T, min=0.0))
|
||||||
|
- torch.sqrt(torch.clamp(centered @ inv_C_h @ centered.T, min=0.0))
|
||||||
|
# scores= mahalanobis_distance(torch.from_numpy(embed_generated[:, layer, :]).cuda(), torch.from_numpy(mean_recorded).cuda(), C_) torch.clamp(centered @ inv_C_t @ centered.T, min=0.0)
|
||||||
|
# - mahalanobis_distance(torch.from_numpy(embed_generated[:, layer, :]).cuda(), torch.from_numpy(mean_t).cuda(), C_t)
|
||||||
|
# + mahalanobis_distance(torch.from_numpy(embed_generated[:, layer, :]).cuda(), torch.from_numpy(mean_h).cuda(), C_h) centered @ inv_C_h @ centered.T
|
||||||
|
|
||||||
scores = torch.mean(scores, -1, keepdim=True)
|
scores = torch.mean(scores, -1, keepdim=True)
|
||||||
scores = torch.sqrt(torch.sum(torch.square(scores), dim=1))
|
scores = torch.sqrt(torch.sum(torch.square(scores), dim=1))
|
||||||
|
|
||||||
projection=V_p[:k, :].T
|
# projection=V_p[:k, :].T
|
||||||
scores1 = torch.mean(centered @ projection, -1, keepdim=True)
|
# scores1 = torch.mean(centered @ projection, -1, keepdim=True)
|
||||||
scores1 = torch.sqrt(torch.sum(torch.square(scores1), dim=1))
|
# scores1 = torch.sqrt(torch.sum(torch.square(scores1), dim=1))
|
||||||
|
|
||||||
|
|
||||||
print(torch.isnan(torch.sqrt(centered @ inv_C @ centered.T)).any())
|
|
||||||
print(torch.isnan(((torch.from_numpy(embed_generated[:, layer, :]-mean_t).cuda() @ torch.linalg.pinv(C_t) @ torch.from_numpy(embed_generated[:, layer, :]-mean_t).cuda().T)** 0.5).any()))
|
|
||||||
print(torch.isnan(((torch.from_numpy(embed_generated[:, layer, :]-mean_h).cuda() @ torch.linalg.pinv(C_h) @ torch.from_numpy(embed_generated[:, layer, :]-mean_h).cuda().T)** 0.5).any()))
|
|
||||||
|
|
||||||
|
|
||||||
# not sure about whether true and false data the direction will point to,
|
# not sure about whether true and false data the direction will point to,
|
||||||
|
|
@ -444,7 +448,7 @@ def main():
|
||||||
best_auroc = measures[0]
|
best_auroc = measures[0]
|
||||||
best_result = [100 * measures[2], 100 * measures[0]]
|
best_result = [100 * measures[2], 100 * measures[0]]
|
||||||
best_layer = layer
|
best_layer = layer
|
||||||
|
best_scores = sign_layer * scores
|
||||||
best_mean = mean_recorded
|
best_mean = mean_recorded
|
||||||
best_sign = sign_layer
|
best_sign = sign_layer
|
||||||
print('k: ', k, 'best result: ', best_result, 'layer: ', best_layer,
|
print('k: ', k, 'best result: ', best_result, 'layer: ', best_layer,
|
||||||
|
|
@ -456,7 +460,7 @@ def main():
|
||||||
best_layer_over_k = best_layer
|
best_layer_over_k = best_layer
|
||||||
best_k = k
|
best_k = k
|
||||||
best_sign_over_k = best_sign
|
best_sign_over_k = best_sign
|
||||||
# best_scores_over_k = best_scores
|
best_scores_over_k = best_scores
|
||||||
# best_projection_over_k = best_projection
|
# best_projection_over_k = best_projection
|
||||||
best_mean_over_k = best_mean
|
best_mean_over_k = best_mean
|
||||||
|
|
||||||
|
|
@ -534,7 +538,7 @@ def main():
|
||||||
# 1, 11, mean=0, svd=0, weight=args.weighted_svd)
|
# 1, 11, mean=0, svd=0, weight=args.weighted_svd)
|
||||||
# get the best hyper-parameters on validation set
|
# get the best hyper-parameters on validation set
|
||||||
returned_results = svd_embed_score(embed_generated_eval, gt_label_val, embed_generated_hal,embed_generated_tru,
|
returned_results = svd_embed_score(embed_generated_eval, gt_label_val, embed_generated_hal,embed_generated_tru,
|
||||||
1, 11, mean=1, svd=10)
|
1, 15, mean=1, svd=10)
|
||||||
|
|
||||||
pca_model = PCA(n_components=returned_results['k'], whiten=False).fit(embed_generated_wild[:,returned_results['best_layer'],:])
|
pca_model = PCA(n_components=returned_results['k'], whiten=False).fit(embed_generated_wild[:,returned_results['best_layer'],:])
|
||||||
projection = pca_model.components_.T
|
projection = pca_model.components_.T
|
||||||
|
|
|
||||||
|
|
@ -62,7 +62,7 @@ def main():
|
||||||
|
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument('--model_name', type=str, default='step-1-8k')
|
parser.add_argument('--model_name', type=str, default='step-1-8k')
|
||||||
parser.add_argument('--dataset_name', type=str, default='tqa')
|
parser.add_argument('--dataset_name', type=str, default='triviaqa')
|
||||||
parser.add_argument('--num_gene', type=int, default=1)
|
parser.add_argument('--num_gene', type=int, default=1)
|
||||||
parser.add_argument('--use_api', type=bool, default=True)
|
parser.add_argument('--use_api', type=bool, default=True)
|
||||||
parser.add_argument('--most_likely', type=bool, default=True)
|
parser.add_argument('--most_likely', type=bool, default=True)
|
||||||
|
|
@ -175,6 +175,7 @@ def main():
|
||||||
raise ValueError("Invalid dataset name")
|
raise ValueError("Invalid dataset name")
|
||||||
f = open(args.instruction, 'r', encoding="utf-8")
|
f = open(args.instruction, 'r', encoding="utf-8")
|
||||||
instruction = f.read()
|
instruction = f.read()
|
||||||
|
error_output='No output'
|
||||||
|
|
||||||
if args.use_api:
|
if args.use_api:
|
||||||
begin_index = 0
|
begin_index = 0
|
||||||
|
|
@ -210,8 +211,9 @@ def main():
|
||||||
hallucination_prompt=get_hal_prompt(dataset[int(used_indices[i])]['context'],question,instruction)
|
hallucination_prompt=get_hal_prompt(dataset[int(used_indices[i])]['context'],question,instruction)
|
||||||
truth_prompt=get_truth_prompt(dataset[int(used_indices[i])]['context'],question)
|
truth_prompt=get_truth_prompt(dataset[int(used_indices[i])]['context'],question)
|
||||||
elif args.dataset_name == 'triviaqa':
|
elif args.dataset_name == 'triviaqa':
|
||||||
prompt = get_qa_prompt("None",dataset[i]['prompt'])
|
prompt = get_qa_prompt("None",dataset[i]['question'])
|
||||||
hallucination_prompt=get_hal_prompt("None",dataset[i]['prompt'],instruction)
|
question= dataset[i]['question']
|
||||||
|
hallucination_prompt=get_hal_prompt("None",dataset[i]['question'],instruction)
|
||||||
truth_prompt=get_truth_prompt("None",question)
|
truth_prompt=get_truth_prompt("None",question)
|
||||||
elif args.dataset_name == 'coqa':
|
elif args.dataset_name == 'coqa':
|
||||||
prompt = get_qa_prompt("None",dataset[i]['prompt'])
|
prompt = get_qa_prompt("None",dataset[i]['prompt'])
|
||||||
|
|
@ -223,31 +225,46 @@ def main():
|
||||||
|
|
||||||
for gen_iter in range(args.num_gene):
|
for gen_iter in range(args.num_gene):
|
||||||
if args.most_likely:
|
if args.most_likely:
|
||||||
response = client.chat.completions.create(
|
try:
|
||||||
|
response = client.chat.completions.create(
|
||||||
model = args.model_name,
|
model = args.model_name,
|
||||||
messages = prompt,
|
messages = prompt,
|
||||||
max_tokens=256,
|
max_tokens=256,
|
||||||
top_p=1,
|
top_p=1,
|
||||||
temperature = 1,
|
temperature = 1,
|
||||||
)
|
)
|
||||||
hallucination_response = client.chat.completions.create(
|
decoded=response.choices[0].message.content
|
||||||
|
except openai.APIStatusError as e:
|
||||||
|
print("error occured!"+str(gen_iter)+"responce {e}")
|
||||||
|
decoded = error_output
|
||||||
|
try:
|
||||||
|
hallucination_response = client.chat.completions.create(
|
||||||
model = args.model_name,
|
model = args.model_name,
|
||||||
messages = hallucination_prompt,
|
messages = hallucination_prompt,
|
||||||
max_tokens=256,
|
max_tokens=256,
|
||||||
top_p=1,
|
top_p=1,
|
||||||
temperature = 1,
|
temperature = 1,
|
||||||
)
|
)
|
||||||
|
hallucination_decoded=hallucination_response.choices[0].message.content
|
||||||
|
except openai.APIStatusError as e:
|
||||||
|
print("error occured!"+str(gen_iter)+"hallucination_responce {e}")
|
||||||
|
hallucination_decoded = error_output
|
||||||
if args.dataset_name == 'tydiqa' or args.dataset_name == 'tydiqa':
|
if args.dataset_name == 'tydiqa' or args.dataset_name == 'tydiqa':
|
||||||
truth_response=client.chat.completions.create(
|
try:
|
||||||
|
truth_response=client.chat.completions.create(
|
||||||
model = args.model_name,
|
model = args.model_name,
|
||||||
messages = truth_prompt,
|
messages = truth_prompt,
|
||||||
max_tokens=256,
|
max_tokens=256,
|
||||||
top_p=1,
|
top_p=1,
|
||||||
temperature=1
|
temperature=1
|
||||||
)
|
)
|
||||||
truth_decoded=truth_response.choices[0].message.content
|
truth_decoded=truth_response.choices[0].message.content
|
||||||
decoded=response.choices[0].message.content
|
except openai.APIStatusError as e:
|
||||||
hallucination_decoded=hallucination_response.choices[0].message.content
|
print("error occured!"+str(gen_iter)+"truth_responce {e}")
|
||||||
|
truth_decoded =error_output
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
response = client.chat.completions.create(
|
response = client.chat.completions.create(
|
||||||
model = args.model_name,
|
model = args.model_name,
|
||||||
|
|
@ -275,10 +292,10 @@ def main():
|
||||||
top_p=0.5,
|
top_p=0.5,
|
||||||
temperature = 0.5,
|
temperature = 0.5,
|
||||||
)
|
)
|
||||||
truth_decoded=truth_response.choices[0].message.content
|
truth_decoded=truth_response.choices[0].message.content
|
||||||
decoded=response.choices[0].message.content
|
decoded=response.choices[0].message.content
|
||||||
hallucination_decoded=hallucination_response.choices[0].message.content
|
hallucination_decoded=hallucination_response.choices[0].message.content
|
||||||
time.sleep(20)
|
time.sleep(40)
|
||||||
|
|
||||||
|
|
||||||
# decoded = tokenizer.decode(generated[0, prompt.shape[-1]:],
|
# decoded = tokenizer.decode(generated[0, prompt.shape[-1]:],
|
||||||
|
|
|
||||||
|
|
@ -50,14 +50,14 @@ def main():
|
||||||
|
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument('--model', type=str, default='llama2_chat_7B')
|
parser.add_argument('--model', type=str, default='llama2_chat_7B')
|
||||||
parser.add_argument('--model_name', type=str, default='step-1-8k')
|
parser.add_argument('--model_name', type=str, default='moonshot-v1-8k')
|
||||||
parser.add_argument('--dataset_name', type=str, default='tqa')
|
parser.add_argument('--dataset_name', type=str, default='tqa')
|
||||||
parser.add_argument('--num_gene', type=int, default=1)
|
parser.add_argument('--num_gene', type=int, default=1)
|
||||||
parser.add_argument('--use_api', type=bool, default=False)
|
parser.add_argument('--use_api', type=bool, default=False)
|
||||||
parser.add_argument('--most_likely', type=bool, default=True)
|
parser.add_argument('--most_likely', type=bool, default=False)
|
||||||
parser.add_argument("--model_dir", type=str, default=None, help='local directory with model data')
|
parser.add_argument("--model_dir", type=str, default=None, help='local directory with model data')
|
||||||
parser.add_argument("--instruction", type=str, default=None, help='local directory of instruction file.')
|
parser.add_argument("--instruction", type=str, default=None, help='local directory of instruction file.')
|
||||||
parser.add_argument('--use_rouge', type=bool, default=False)
|
parser.add_argument('--use_rouge', type=bool, default=True)
|
||||||
parser.add_argument('--thres_gt', type=float, default=0.5)
|
parser.add_argument('--thres_gt', type=float, default=0.5)
|
||||||
|
|
||||||
# parser.add_argument('--model_name', type=str, default='llama2_chat_7B')
|
# parser.add_argument('--model_name', type=str, default='llama2_chat_7B')
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue