"は“ is a marker for "emphasis", which turns to mean "topic".
like :
「これはうまい!」
「あいつはなぐる」
「昨日は雨だが、今日は晴れ」
「象は鼻が長い」
「中国は人口が多い」
「男は度胸、女は愛嬌」
The function of the は in 『分かってはいた』 is "emphasis"
『分かってはいた』 is an emphasized form of 『分かっていた(I understood/I knew)』
So what kind of emphasis is it?
Why does this emphasis lead to mean "but"?
If, when you eat an apple and an orange, you only say "This apple tastes so good!. Greatest as I've ever eaten!" but you don't mention anything about the orange, people will naturally think "ah, the apple is great but this orange wasn't so good".
The same thing is happening in the emphasized sentence "分かってはいた”. Hearing this word, people would think "OK, he understood it, but probably he was not able to do that".
You often use this form when you "lie" to yourself, prentending not to know the truth.
「やらなきゃいけないと分かってはいた(でもやらなかった)」
「あんたがオレのことを好きじゃないって、分かってはいた
(でもあきらめられなかった / でも気づかないふりをしていた)」
「あんたがオレの方ばかり見ていたこと、分かってはいた
(でも気づかないふりをしていた)」
「どうせ無理だと分かってはいた(でもあきらめられなかった)」
* 分かっていた and 分かってはいた are 99.9% interchangeable because it is only an emphasis.
------------------------------------------------------------------
* 分かってはいた consists of :
(1). 分かっ (=分かり ), 連用形 from a verb 分かる
(2). て,a marker for "completion/fix/confirmation"
(3). は, a marker for "emphasis"
(4). い, 連用形 from a verb いる
(5). た, auxiliary verb for "completion/fix/confirmation"
The (2) て and (5). た work as well for emphasis because they indicate "completion/fix/confirmation"
* You can think of て as 連用形 from た.
They mean the same after all.